我正在使用Jersey 2.19来实现REST API,但是我很难使用@QueryParam从POST请求中提取查询参数,即使我正在调用我的资源方法 .
这是我的资源方法:
@POST
@Produces(MediaType.TEXT_PLAIN)
public Response test(@QueryParam("test-param") String testParam)
{
String response = "testParam is: " + testParam + "\n";
return Response.status(Response.Status.OK).entity(response).build();
}
我正在使用cURL提交HTTP POST请求,如下所示:
curl -X POST http://192.168.0.2:8080/myApp/test --data test-param=Hello
返回的值始终为null .
我究竟做错了什么?
2 回答
curl中的--data将提供整个文本
test-param=Hello
. 请求它的正确方法是:curl -X POST http://192.168.0.2:8080/myApp/test?test-param=Hello
尝试使用curl -X POST'192.168.0.2:8080/myApp/test?test-param=Hello';
-d, - data
(HTTP)将POST请求中的指定数据发送到HTTP服务器,就像用户填写HTML表单并按下提交按钮时浏览器一样 . 这将导致curl使用content-type application / x-www-form-urlencoded将数据传递到服务器 . 比较-F, - form .