【问题标题】:URL parameters are not being passed by curl POSTcurl POST 未传递 URL 参数
【发布时间】:2026-02-23 03:05:01
【问题描述】:

这是我的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

你可以看到最后的零:(

【问题讨论】:

  • 应该是x=5&y=3curl 是否像您认为的那样解析 :
  • @SotiriosDelimanolis 与 :& 相同
  • @SotiriosDelimanolis 请提供任何帮助
  • 也许可以尝试 2 件事,1) 使用 @FormParam 2) 在 -X 和 post 之间添加一个空格
  • 我也没有在手册页中看到 : 与 = 相同,也许也可以试试 curl.haxx.se/docs/manpage.html

标签: java curl jersey http-post


【解决方案1】:

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

【讨论】:

  • 在回答您的问题之前,我需要澄清一下,您说:since you are not sending any data in the body, you should probably just make the resource method a GET method. 但是等等,我正在发送帖子请求,这意味着 xy 参数不在正文中在标题中,对吧?
  • 也许我应该说的是“因为资源方法不期望正文中有任何数据”。您没有接受主体的方法参数。通常,接受主体的方法参数没有注释。在大多数情况下,这就是它被称为实体主体的方式(表单数据除外)。在您的情况下,数据预计将来自 URL (@QueryParam)
  • 好吧,很明显我在使用 queryparam 时犯了一个错误,我不知道 queryparam 是用于 url。但是如果我将其更改为参数,这是否意味着 x 和 y 在 http 请求的正文中?
  • 是的。使用-d,将其发送到正文中。那么第一个例子就是你要使用的。
  • 我明白-d '{"x":3, "y":4}'不代表请求是json吗?现在加一个
【解决方案2】:

您缺少命令的数据部分:

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

-d(或--data)应该在链接之前。 并且“名称值对”应该是 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--data 的缩写形式,并且(或者)将动词更改为POST,因此您不需要-X(尽管它没有伤害)。关键是像你说的那样把它放在URL之前,并使用=。如果您使用--data-urlencode curl 会为您进行 URL 编码。 (不是 HTML 实体,例如 "  并且不用于仅输入输出。)
  • @dave_thompson_085 我的错误,我混淆了 2 个命令,将修复答案
  • 根据你的回答,请求应该是curl -X POST -d "x=4&y=2" "http://localhost:8080/CurlServer/curl/curltutorial/sumPost",但我还是有同样的问题
  • @MarcoDinatsoli 编辑您的问题,发布以下内容:http://localhost:8080/CurlServer/curl/curltutorial/sumPost 使用浏览器访问,并检查链接是否按预期工作。然后,从服务器发布访问日志,显示请求是正确的(200 代码,来自浏览器和来自应用程序)。最后显示结果:curl --data "x=4&y=2" http://localhost:8080/CurlServer/curl/curltutorial/sumPost复制/粘贴,如下所示... URL+URI 不应在"
  • @Bonatti 我已经做了所有这些,四个小时前,请检查我的更新。
【解决方案3】:

你有没有试过这样称呼它:

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

?

【讨论】: