curl POST 未传递 URL 参数

2022-01-21 00:00:00 http-post curl java jersey

这是我的java代码:

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) + "
";
}

我这样称呼它:

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

问题是 System.out.println 调用一直发布零零,看来我没有正确传递 x 和 y.

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

得到答复后,我将请求改为:

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

服务是:

@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 :(

推荐答案

-dx=1&y=2(注意=,而不是:) 是表单数据 (application/x-www-form-urlencoded) 将请求的正文发送给它,其中您的资源方法应该看起来更像

-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) {

}

下面的请求会起作用

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

注意:对于 Windows,需要双引号 ("x=5&y=3")

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

你甚至可以分离键值对

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

默认的Content-Typeapplication/x-www-form-urlencoded,所以不需要设置.

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

@QueryParams 应该是 查询字符串(URL 的一部分),而不是正文数据的一部分.所以你的请求应该更像

@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"

尽管如此,由于您没有在正文中发送任何数据,因此您可能应该将资源方法设为 GET 方法.

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) {
}

如果您想发送 JSON,那么最好的办法是确保您有一个处理反序列化为 POJO 的 JSON 提供程序[1].然后你可以有类似的东西

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]- 重要的是您确实有一个 JSON 提供程序.如果您没有,您将收到一条异常消息,例如 No MessageBodyReader found for mediatype application/json and type Operands".我需要知道 Jersey 版本以及您是否使用 Maven,才能确定应该如何添加 JSON 支持.但是对于一般信息,您可以查看

[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

  • 在 JAX-RS 中将 JSON 解组为 Java POJO

相关文章