URL curl 未传递参数 POST

URL parameters are not being passed by curl POST

这是我的 java 代码:

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

我这样称呼它:

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

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

更新

回答后,我的要求改为:

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

但我仍然遇到同样的问题。这是服务器的响应:

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

你可以看到最后的零:(

您缺少命令的数据部分:

curl --data "param1=value1&param2=value2" https://example.com/fakesite.php

-d(或 --data)应该在 link 之前。 "name value pair" 应该是 varName=varValue&otherVar=otherValue

此外,根据文档,-X 命令不正确:

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option. 

应该是-X POST

最后,请记住使用 "html encode" 对您的值进行编码。

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

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

你甚至可以分开键值对

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

默认的Content-Typeapplication/x-www-form-urlencoded,不用设置。

@QueryParams 应该是 query string 的一部分(URL 的一部分),而不是正文数据的一部分。所以你的要求应该更像

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

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

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

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

的东西
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 支持。但对于一般信息,您可以查看

你试过这样称呼它吗:

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

?