如何使用 cURL POST JSON 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7172784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I POST JSON data with cURL?
提问by kamaci
I use Ubuntu and installed cURLon 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 端编写了我的 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}
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 PUTcode (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;
}
回答by Sean Patrick Floyd
You need to set your content-type to application/json. But -dsends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring's side.
您需要将内容类型设置为 application/json。但是-d发送 Content-Type application/x-www-form-urlencoded,这在 Spring 方面是不被接受的。
Looking at the curl man page, I think you can use -H:
-H "Content-Type: application/json"
Full example:
完整示例:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login
(-His short for --header, -dfor --data)
(-H是--header, -dfor 的缩写--data)
Note that -request POSTis optionalif you use -d, as the -dflag implies a POST request.
请注意,如果您使用,这-request POST是可选的-d,因为该-d标志意味着一个 POST 请求。
On Windows, things are slightly different. See the comment thread.
在 Windows 上,情况略有不同。请参阅评论线程。
回答by Typisch
Try to put your data in a file, say body.jsonand then use
尝试将您的数据放在一个文件中,body.json然后使用
curl -H "Content-Type: application/json" --data @body.json http://localhost:8080/ui/webapp/conf
回答by mo-seph
You might find resty useful: https://github.com/micha/resty
你可能会发现 resty 很有用:https: //github.com/micha/resty
It's a wrapper round CURL which simplifies command line REST requests. You point it to your API endpoint, and it gives you PUT and POST commands. (Examples adapted from the homepage)
它是一个环绕 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
Also, it's often still necessary to add the Content Type headers. You can do this once, though, to set a default, of add config files per-method per-site: Setting default RESTY options
此外,通常仍然需要添加内容类型标头。但是,您可以执行一次此操作以设置默认值,即为每个站点的每个方法添加配置文件:设置默认 RESTY 选项
回答by venkatnz
For Windows, having a single quote for the -dvalue did not work for me, but it did work after changing to double quote. Also I needed to escape double quotes inside curly brackets.
对于 Windows,对-d值使用单引号对我不起作用,但在更改为双引号后确实有效。我还需要在大括号内转义双引号。
That is, the following did not work:
也就是说,以下方法不起作用:
curl -i -X POST -H "Content-Type: application/json" -d '{"key":"val"}' http://localhost:8080/appname/path
But the following worked:
但以下工作:
curl -i -X POST -H "Content-Type: application/json" -d "{\"key\":\"val\"}" http://localhost:8080/appname/path
回答by Luis
It worked for me using:
它对我有用:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/api/postJsonReader.do
It was happily mapped to the Spring controller:
它很高兴地映射到 Spring 控制器:
@RequestMapping(value = "/postJsonReader", method = RequestMethod.POST)
public @ResponseBody String processPostJsonData(@RequestBody IdOnly idOnly) throws Exception {
logger.debug("JsonReaderController hit! Reading JSON data!"+idOnly.getId());
return "JSON Received";
}
IdOnlyis a simple POJOwith an id property.
IdOnly是一个带有 id 属性的简单POJO。
回答by Eduardo Cerqueira
As an example, create a JSON file, params.json, and add this content to it:
例如,创建一个 JSON 文件 params.json,并向其中添加以下内容:
[
{
"environment": "Devel",
"description": "Machine for test, please do not delete!"
}
]
Then you run this command:
然后你运行这个命令:
curl -v -H "Content-Type: application/json" -X POST --data @params.json -u your_username:your_password http://localhost:8000/env/add_server
回答by Steffen Roller
I just run into the same problem. I could solve it by specifying
我只是遇到了同样的问题。我可以通过指定来解决它
-H "Content-Type: application/json; charset=UTF-8"
回答by felipealvesgnu
This worked well for me.
这对我来说效果很好。
curl -X POST --data @json_out.txt http://localhost:8080/
Where,
在哪里,
-XMeans the http verb.
-X表示http动词。
--dataMeans the data you want to send.
--data表示您要发送的数据。
回答by kiltek
You can use Postmanwith its intuitive GUI to assemble your cURLcommand.
您可以使用Postman及其直观的 GUI 来组合您的cURL命令。
- Install and Start Postman
- Type in your URL, Post Body, Request Headers etc. pp.
- Click on
Code - Select
cURLfrom the drop-down list - copy & paste your
cURLcommand
- 安装并启动Postman
- 输入您的 URL、帖子正文、请求标题等。
- 点击
Code - 选择
cURL从下拉列表 - 复制并粘贴您的
cURL命令
Note: There are several options for automated request generation in the drop-down list, which is why I thought my post was neccessary in the first place.
注意:下拉列表中有几个自动生成请求的选项,这就是为什么我认为我的帖子首先是必要的。
回答by Márcio Brener
Using CURL Windows, try this:
使用 CURL Windows,试试这个:
curl -X POST -H "Content-Type:application/json" -d "{\"firstName\": \"blablabla\",\"lastName\": \"dummy\",\"id\": \"123456\"}" http-host/_ah/api/employeeendpoint/v1/employee

