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

当前回答

-H在标头中发送类似内容类型或身份验证令牌的内容-d此处添加您的数据最后添加站点链接

注意:不要忘记为身份验证凭据添加身份验证令牌(如果有)

curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Token 2de403987713595a7955a9b4655b9e206d4294b3' -d '{"title":"Post test with curl", "body": "test body"}' http://127.0.0.1:8000/api/v1/feeds/

其他回答

你可能会发现resty很有用:

它是CURL的包装器,简化了命令行REST请求。您将其指向API端点,它将为您提供PUT和POST命令(示例改编自主页)。

resty http://127.0.0.1:8080/data #Sets up resty to point at your endpoing
GET /blogs.json                  #Gets http://127.0.0.1:8080/data/blogs.json
                                 #Put JSON
PUT /blogs/2.json '{"id" : 2, "title" : "updated post", "body" : "This is the new."}'
                                 # POST JSON from a file
POST /blogs/5.json < /tmp/blog.json

此外,通常仍然需要添加Content-Type标头。但是,您可以这样做一次,以设置每个站点每个方法添加配置文件的默认值:设置默认RESTY选项

TL;博士:

使用神圣的三位一体,jo+curl+jq(或fx):

jo value=30 type="Tip 3" targetModule="Target 3" configurationGroup=null name="Configuration Deneme 3" description=null identity="Configuration Deneme 3" | \
curl --json @- \
    -X POST \
    http://localhost:8080/xx/xxx/xxxx | \
jq

这将涵盖丢失的必要标头:无需显式定义Content-Type和Accept标头。

使用--json的新卷曲方式

2022年3月初,curl发布了一个新的命令行参数——json,版本为7.82.0。这允许通过JSON发送快捷方式,并且无需定义缺少的ContentType和Accept标头,因为这些标头是自动假定的,因此减少了错误风险:

curl --json '{"tool": "curl"}' https://example.com/

但等等。。。还有更多。不要将json参数定义为curl命令行的字符串,而是使用漂亮的joCLI工具将json定义为一系列键-值对,并通过curl管道输出。仅使用jo来定义JSON,其工作方式如下:

 > jo -p 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
{
   "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命令来展示这一点,但没有额外的头,并使用jo+jq来实现良好的输出:

jo value=30 type="Tip 3" targetModule="Target 3" configurationGroup=null name="Configuration Deneme 3" description=null identity="Configuration Deneme 3" | \
curl --json @- \
    -X POST \
    http://localhost:8080/xx/xxx/xxxx | \
jq

免费API示例

使用免费模拟API进行演示:

> jo title="Blog Post" body="lorem ipsum" userId=33 | \
curl --json @- \
    -X POST \
    https://jsonplaceholder.typicode.com/posts | \
jq

由于jq,输出格式很好:

{
  "title": "Blog Post",
  "body": "lorem ipsum",
  "userId": 33,
  "id": 101
}

对于我使用过的PowerShell:

curl.exe -H "Content-Type: application/json" --data "@content.json" http://localhost:8080/appname/path

其中content.json是包含请求的本地json文件的名称,curl.exe而不是curl,不使用InvokeWebRequest的别名。

或者如果您想直接指定JSON:

curl.exe -H "Content-Type: application/json" --data '{\"username\":\"xyz\",\"password\":\"xyz\"}' http://localhost:8080/appname/path

例如,创建一个JSON文件params.JSON,并向其中添加以下内容:

[
    {
        "environment": "Devel",
        "description": "Machine for test, please do not delete!"
    }
]

然后运行以下命令:

curl -v -H "Content-Type: application/json" -X POST --data @params.json -u your_username:your_password http://localhost:8000/env/add_server

以下代码适用于我。

我使用的是示例数据api

curl -X POST --data @json_out.txt https://sampledataapi.com/API/login

这里是解释

-X Means the HTTP verb.
--data Means the data you want to send.