我使用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;
}

当前回答

--json<data>将POST请求中指定的json数据发送到HTTP服务器。

卷曲7.82.0+

# Send a basic JSON object
curl --json '{"name":"xyz","breed":"xyz","age":100}' http://127.0.0.1:3000/cats

# letter @, read the data from a file
curl --json @cat.txt http://127.0.0.1:3000/cats

# letter -, read the data from stdin
echo '{"name":"xyz","breed":"xyz","age":100}' | curl --json @- http://127.0.0.1:3000/cats

卷曲7.82.0-

curl -X POST --header "Content-Type: application/json" --data '{"name":"xyz","breed":"xyz","age":100}' http://127.0.0.1:3000/cats

https://curl.se/docs/manpage.html#--json文件

其他回答

您还可以将JSON内容放入文件中,并通过标准输入使用--uploadfile选项将其传递给curl,如下所示:

echo 'my.awesome.json.function({"do" : "whatever"})' | curl -X POST "http://url" -T -

如果您正在针对RESTful接口测试大量JSON发送/响应,您可能需要查看Postman插件for Chrome(允许您手动定义web服务测试)及其基于Node.js的Newman命令行同伴(允许您针对Postman测试的“集合”自动测试)。免费且开放!

您可以通过--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@-

如果要包含动态数据,这里还有另一种方法。

#!/bin/bash

version=$1
text=$2
branch=$(git rev-parse --abbrev-ref HEAD)
repo_full_name=$(git config --get remote.origin.url | sed 's/.*:\/\/github.com\///;s/.git$//')
token=$(git config --global github.token)

generate_post_data()
{
  cat <<EOF
{
  "tag_name": "$version",
  "target_commitish": "$branch",
  "name": "$version",
  "body": "$text",
  "draft": false,
  "prerelease": false
}
EOF
}

echo "Create release $version for repo: $repo_full_name branch: $branch"
curl --data "$(generate_post_data)" "https://api.github.com/repos/$repo_full_name/releases?access_token=$token"

问题在这里:

HTTP/1.1 415 Unsupported Media Type

服务器登录无法解释此请求的媒体类型,因此将其解析为text/html

任何资源的媒体类型都在内容类型中声明请求标头的属性

“接受”。。。header将使该请求失败,因此发送任何JSON请求都需要以下内容,即内容类型

-H 'content-type: application/json'

假设数据和URL类似于

{“电子邮件”:“admin@admin.com“,”密码“:”123456“}

http://localhost:5000/api/login

然后在Linux中

curl  http://localhost:5000/api/login  -H 'content-type: application/json'  -d '{"email": "user@admin.com", "password": "123456"}'

在Windows中(参数周围的单引号不起作用)

curl  http://localhost:5000/api/login  -H "content-type: application/json"  -d "{\"email\": \"user@admin.com\", \"password\": \"123456\"}"

-当命令中存在-d{…..}时,不需要X POST密钥。

对于PUT请求:

-X PUT