Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

java - URL parameters are not being passed by curl POST

This is my java code:

@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam(value = "x") int x,
        @QueryParam(value = "y") int y) {
    System.out.println("x = " + x);
    System.out.println("y = " + y);
    return (x + y) + "
";
}

I call it like this:

curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x:5&y:3'

The problem is the System.out.println call keeps posting zero zero, it seems I am not passing x and y correctly.

Update

After the answer, I changed my request to:

curl   -d '{"x" : 4, "y":3}'  "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -H "Content-Type:application/json" -H "Accept:text/plain"  --include

and the service is:

@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(@QueryParam(value = "x") int x,
        @QueryParam(value = "y") int y) {
    System.out.println("sumPost");
    System.out.println("x = " + x);
    System.out.println("y = " + y);
    return (x + y) + "
";
}

but I still have the same problem. Here is the response from the server:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Wed, 23 Sep 2015 11:12:38 GMT

0

You can see the zero at the end :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

-d x=1&y=2 (notice the =, not :) is form data (application/x-www-form-urlencoded) sent it the body of the request, in which your resource method should look more like

@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String sumPost(@FormParam("x") int x,
                      @FormParam("y") int y) {

}

and the following request would work

curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x=5&y=3'

Note: With Windows, double quotes are required ("x=5&y=3")

You could even separate the key value pairs

curl -XPOST "http://localhost:8080/..." -d 'x=5' -d 'y=3'

The default Content-Type is application/x-www-form-urlencoded, so you don't need to set it.

@QueryParams are supposed to be part of the query string (part of the URL), not part of the body data. So your request should be more like

curl "http://localhost:8080/CurlServer/curl/curltutorial/sumPost?x=1&y=2"

With this though, since you are not sending any data in the body, you should probably just make the resource method a GET method.

@GET
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam("x") int x,
                      @QueryParam("y") int y) {
}

If you wanted to send JSON, then your best bet is to make sure you have a JSON provider[1] that handle deserializing to a POJO. Then you can have something like

public class Operands {
    private int x;
    private int y;
    // getX setX getY setY
}
...
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(Operands ops) {

}

[1]- The important thing is that you do have a JSON provider. If you don't have one, you will get an exception with a message like "No MessageBodyReader found for mediatype application/json and type Operands". I would need to know what Jersey version and if you are using Maven or not, to able to determine how you should add JSON support. But for general information you can see


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...