gpt4 book ai didi

How do I POST JSON data with cURL?(如何使用cURL发布JSON数据?)

转载 作者:bug小助手 更新时间:2023-10-26 20:27:13 27 4
gpt4 key购买 nike



I use Ubuntu and installed cURL on it. I want to test my Spring REST application with cURL. I wrote my POST code at the Java side. However, I want to test it with cURL. I am trying to post a JSON data. Example data is like this:

我使用Ubuntu,并在上面安装了Curl。我想用cURL测试我的Spring rest应用程序。我在Java端编写了我的邮政编码。但是,我想用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}

I use this command:

我使用以下命令:


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

It returns this error:

它返回以下错误:


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

The error description is this:

错误描述如下:



The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().



Tomcat log:
"POST /ui/webapp/conf/clear HTTP/1.1" 415 1051

Tomcat日志:“POST/UI/Webapp/conf/Clear HTTP/1.1”415 1051


What is the right format of the cURL command?

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


This is my Java side PUT code (I have tested GET and DELETE and they work):

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

更多回答

checkout the link for spring 3.2.0 post requests.

查看Spring3.2.0 POST请求的链接。

There is a nice post Using Curl For Ad Hoc Testing Of RESTful Microservices which covers this with multiple examples.

有一个很好的帖子,使用Curl对REST风格的微服务进行特别测试,其中通过多个示例介绍了这一点。

By the way, it's better to minify json data before sending to server. Use smart json to is a better choice in 2023.

顺便说一句,最好在将json数据发送到服务器之前将其缩小。2023年,使用智能json是更好的选择。

优秀答案推荐

To POST JSON data with cURL, you should use the -d flag with the data enclosed in single quotes and specify the content type as application/json using the -H flag. Here's the corrected cURL command:

要使用cURL发布JSON数据,您应该使用-d标志,将数据括在单引号中,并使用-H标志将内容类型指定为APPLICATION/json。以下是更正后的curl命令:


curl -i \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-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

Explanation of changes:

变更说明:



  1. Added -H "Content-Type: application/json" to specify that you are sending JSON data in the request body.

  2. Enclosed the JSON data in single quotes (') to ensure that it is treated as a single argument by cURL.

  3. Wrapped the JSON data with {} to create a valid JSON object.


This should resolve the "415 Unsupported Media Type" error, and your Spring REST application should be able to accept the JSON data correctly.

这应该会解决“415 Unsupported Media Type”错误,并且您的Spring rest应用程序应该能够正确地接受JSON数据。



below code works for me.

下面的代码适用于我。


I was using Sample data api

我使用的是示例数据API


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

Here the explanation

以下是解释


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

更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com