I have this controller in spring
#RestController
public class GreetingController {
#RequestMapping(value = "/greeting", method = RequestMethod.POST)
public String greeting(#RequestParam("uouo") String uouo) {
return uouo;
}
}
and when I testing it
curl -k -i -X POST -H "Content-Type:application/json" -d uouo=test http://192.168.1.104:8080/api/greeting
the result of the testing
HTTP Status 400 - Required String parameter 'uouo' is not present
I tried may thing, but I think #RequestParam can't use for POST it always passed the parameter in URL using GET, I use post only if I had object JSON as parameter using #RequestBody, is there any way to make string parameter send using POST?
The Servlet container will only provide parameters from the body for POST requests if the content type is application/x-www-form-urlencoded. It will ignore the body if the content type is anything else. This is specified in the Servlet Specification Chapter 3.1.1 When Parameters Are Available
The following are the conditions that must be met before post form
data will be populated to the parameter set:
The request is an HTTP or HTTPS request.
The HTTP method is POST.
The content type is application/x-www-form-urlencoded.
The servlet has made an initial call of any of the getParameter family of methods on the request object.
If the conditions are not met and the post form data is not included
in the parameter set, the post data must still be available to the
servlet via the request object’s input stream. If the conditions are
met, post form data will no longer be available for reading directly
from the request object’s input stream.
Since you aren't sending any JSON, just set the appropriate content type
curl -k -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d uouo=test http://192.168.1.104:8080/api/greeting
or let curl infer it
curl -k -i -X POST -d uouo=test http://192.168.1.104:8080/api/greeting?uouo=test
Note that you can still pass query parameters in the URL
curl -k -i -X POST -H "Content-Type:application/json" http://192.168.1.104:8080/api/greeting?uouo=test
Related
I'm attempting to verify a slack request in spring boot 2.1. I can correctly compute the hash in a unit test. However, I cannot get the verification to work when the server handles an actual request. This is because the order of the url parameters changes between the initial request and the time the controller is invoked.
You can reproduce this using the example controller and curl below. Notice that the form payload in the curl is hello=world&foo=baz&two=three and the response is reordered to two=three&foo=baz&hello=world.
Theory
I believe this is happening here. Spring detects that form data has been posted, then encodes the form parameters, and writes them to a new input stream. It appears that the map that is iterated over to encode the params does not retain the original order.
Question
Is there a way to obtain the original post body?
Reproduce
#Controller
#ResponseBody
public class SlackController {
#RequestMapping(
path = "slack/command",
method = RequestMethod.POST,
produces = { MediaType.TEXT_PLAIN_VALUE },
consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public String executeCommand(
#RequestBody String requestBody
) {
return requestBody;
}
}
$ curl -X POST 'http://localhost:8080/slack/command' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: text/plain' \
-d 'hello=world&foo=baz&two=three'
HTTP/1.1 200 OK
Date: Thu, 25 Apr 2019 05:26:53 GMT
Content-Type: application/json;charset=iso-8859-1
Content-Length: 29
two=three&foo=baz&hello=world
Hello people i have a question about how to get JSON pagingObject in my response with a http curl get (curl running on jokto-linux-system).
i wanted to get a special page over the parameter $offset=0 and $limit=0.
But i dont know where to put the parameter to get on the right page of the serviceressource. I get all the ressources back but i need to see how much pages are used also i didnt see this too.
So my basic problem is the visibility and changing of the parameter $offset and $limit of the pagingObject of an Service/Ressource with amount of Ressourceelements.
Thank you very much for evry answer
This my basic "GET" i try: curl -i http://127.0.0.1:80/service/ressource/Elementuri
Tried to get an the object over header: curl -X GET -H "$offset" -H "$limit" http://127.0.0.1:80/service/ressource/elementuri
I tried it to set after the uri: curl -X GET http://127.0.0.1:80/service/ressource/elementuri?$offset=0&$limit=1
Dollar sign is probably not necessary.
curl http://127.0.0.1:80/service/ressource/elementuri?limit=2&offset=2
I am making a curl request to endpoint with following option curl --data {"foo" : "bar"} for POST request
However I want to implement a default behavior for missing data, ie curl without --data should also work for POST request.
Currently if data is missing it returns: "error":400,"errorCode":"INVALID_JSON". I do not want that.
Is it possible ?
Does it have any annotation to support optional data.?
How to add bellow curl comment to the jersey client.
curl -X POST --user 'gigy:secret' -d 'grant_type=password&username=peter#example.com&password=password' http://localhost:8000/gigy/oauth/token
I tried to like bellow. but I don't know how to add other things.
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8000/gigy/oauth/token");
--user 'gigy:secret'
You need Basic Authentication. Basically You need to set the Authorization header with the value Basic base64("gigy:secret"), where base64 is whatever you use to convert the string "user:password" to its Base 64 counterpart. You can set headers on the WebResource calling one it's header method.
-d 'grant_type=password&username=peter#example.com&password=password'
These are application/x-www-form-urlencoded parameters. This is what you will need to send as the entity body of the request. With Jersey you can use the com.sun.jersey.api.representation.Form class. Once you create it, just add key value/pairs to it like key=grant_type and value=password. All the pairs split by &.
Implicit Media type.
When you don't set the Content-Type header in your cURL request, a POST will default to application/x-www-form-urlencoded. You need to set this using the type(MediaType) function after you call header. Use MediaType.APPLICATION_FORM_URLENCODED_TYPE.
-X POST
Now you need to send the request. Just call post after you call type, with the following arguments .post(ClientResponse.class, yourForm). This will return a ClientResponse.
This question already has answers here:
HTTP GET with request body
(23 answers)
Closed 7 years ago.
I don't understand why this curl invocation gives me a 400 bad request.
curl -v -XGET -H "Content-Type:application/json" -d '{"match":{"first":"james"}}' http://localhost:8080/geocon/search/
Considered that this is the code part who should handle everything
#Path(Paths.SEARCH)
public class SearchService {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String search(#DefaultValue(NO_TYPE) #QueryParam("type") String type, String query) {
// do interesting stuff with the query
System.out.println(type);
System.out.println(query);
return //the result of the query
}
Obviously, I don't see in the console the two print lines and the answer from the web service is 400 Bad request
If I change the verb in curl and in the code (POST instead of GET), the results are OK and everything works as it should.
Why?
Can't I use GET passing data? If you think about it, I am not modifying stuff, I'd just receive data so GET should be the most RESTful translation for it.
You are allowed to send a body with a GET request, but it must not have any semantics; it should essentially be ignored. Not doing so violates the HTTP/1.1 specification. I suggest avoiding this practice because no one expects a request body with a GET request.
Now I'm not sure if the MVC framework you're using is stricter in the sense that it automatically rejects any GET request sent with a body, because in general a server can accept a GET request with a body, but is just supposed to ignore it.
Hence, what you're doing is not RESTful. The only way you can pass in data through a GET is through query parameters.
UPDATE
This is in response to your comment.
You could expose an explicit resource called searchResult or search. To create a searchResult or search resource instance, you pass in a body that is essentially a query. If you are using semantic media types (as you should for proper RESTfulness), your search-result resource could have the media type application/vnd.myservice.search-result+json and the query can have the media type application/vnd.myservice.search-query+json. Then, you can POST a request to /searchResults that has a request body that contains the complex query. The media-type of the response would be application/vnd.myservice.search-result+json and it can contain the search results.
So how does this play out? A search query comes in and you parse out the body and run the query. Then you should persist the results (not for ever; give it some sane TTL value). Once you have done that, you return a 303 See Other with a Location header that has a link to the search results (maybe something like /searchResults/4334, where 4334 is the id of this particular result). The client can then access this URI to retrieve the search results. If the client requests a search result that has expired (i.e., the server cleaned it up because the TTL expired), then the server should respond with a 410 Gone.
Also read this for more pointers.
It seems that syntax you are using has a typo.Try using the below ones for json and xml format. -XGET is wrong. it should be -X GET.
For xml
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
For JSON
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource