我使用Ubuntu并在上面安装了cURL。我想用cURL测试我的SpringREST应用程序。我在Java端编写了POST代码。然而,我想用cURL测试它。我正在尝试发布JSON数据。示例数据如下:

{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}

我使用以下命令:

curl -i \
    -H "Accept: application/json" \
    -H "X-HTTP-Method-Override: PUT" \
    -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true \
    http://localhost:8080/xx/xxx/xxxx

它返回以下错误:

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT

错误描述如下:

服务器拒绝了此请求,因为请求实体的格式不受请求方法()的请求资源支持。

Tomcat日志:“POST/ui/webapp/conf/clearHTTP/1.1”415 1051

cURL命令的正确格式是什么?

这是我的Java端PUT代码(我测试过GET和DELETE,它们都有效):

@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
    configuration.setName("PUT worked");
    //todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return configuration;
}

当前回答

对于Windows,对-d值使用单引号对我来说不起作用,但在更改为双引号后,它确实起了作用。我还需要在大括号内转义双引号。

也就是说,以下操作无效:

curl -i -X POST -H "Content-Type: application/json" -d '{"key":"val"}' http://localhost:8080/appname/path

但以下措施奏效了:

curl -i -X POST -H "Content-Type: application/json" -d "{\"key\":\"val\"}" http://localhost:8080/appname/path

其他回答

您需要将内容类型设置为application/json。但是-d(或--data)发送Content-Type应用程序/x-www-form-urlencoded,这在Spring方面不被接受。

查看curl手册页,我认为可以使用-H(或--header):

-H "Content-Type: application/json"

完整示例:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz","password":"xyz"}' \
  http://localhost:3000/api/login

(-H是--header的缩写,-d是--data的缩写)

注意,如果使用-d,-request POST是可选的,因为-d标志表示POST请求。


在Windows上,情况略有不同。请参见注释线程。

您可以通过--dataraw参数将JSON文件的内容分类为curl。

卷曲'https://api.com/route'-H'内容类型:application/json'--数据原始“$(cat~/.json/payload-2022-03-03.json|grep-v'^\s*//')”

注意:JSON文件中的注释通过grep-v“^\s*//”过滤掉

您还可以使用grep或cat通过标准输入将数据传递给curl。

grep-v'^\s*//'~/.json/payload-2022-03-03.json|curl'https://api.com/route'-H'内容类型:application/json'-d@-

cat~/.json/payload-2022-03-03.json|grep-v'^\s*//'|curl'https://api.com/route'-H'内容类型:application/json'-d@-

这在Windows 10上对我有效:

curl -d "{"""owner""":"""sasdasdasdasd"""}" -H "Content-Type: application/json" -X  PUT http://localhost:8080/api/changeowner/CAR4

我使用以下格式在web服务器上进行测试。

use -F 'json data'

让我们假设JSON dict格式:

{
    'comment': {
        'who':'some_one',
        'desc' : 'get it'
    }
}

完整示例

curl -XPOST your_address/api -F comment='{"who":"some_one", "desc":"get it"}'

您可以使用Postman及其直观的GUI来组装cURL命令。

安装并启动Postman键入您的URL、Post Body、Request Headers等。单击代码从下拉列表中选择cURL复制并粘贴cURL命令

注意:下拉列表中有几个自动生成请求的选项,这就是为什么我认为我的文章首先是必要的。