bash cURL 将 JSON 作为 x-www-form-urlencoded 发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52320831/
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
cURL send JSON as x-www-form-urlencoded
提问by Kousha
I want to post the following JSON:
我想发布以下 JSON:
{
"cities": {
"chicago": 123,
"boston": 245
}
}
Using curl
as x-www-form-urlencoded
without using a .json file. I cannot figure out how to build the curl -F ...
使用curl
asx-www-form-urlencoded
而不使用 .json 文件。我不知道如何构建curl -F ...
回答by ikos23
For application/x-www-form-urlencoded
you could try:
因为application/x-www-form-urlencoded
你可以尝试:
curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/blahblah
Where param1=value...
have to be your JSON data as chicago=123&boston=245
哪里param1=value...
必须是你的 JSON 数据chicago=123&boston=245
Or explicit form:
或显式形式:
curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/blahblah
Instead of http://localhost:3000/blahblah
you should provide real URL of your service.
而不是http://localhost:3000/blahblah
您应该提供您的服务的真实 URL。
回答by miken32
The whole point of curl -F
, according to the man page, is "to POST data using the Content-Type multipart/form-data according to RFC 2388." In other words, it's best used when you need to emulate an HTML form with a file input.
curl -F
根据手册页,的全部要点是“根据 RFC 2388 使用 Content-Type multipart/form-data POST 数据”。换句话说,当您需要使用文件输入模拟 HTML 表单时,最好使用它。
Instead use curl -d
to specify the raw POST data:
而是用于curl -d
指定原始 POST 数据:
curl -d '{"cities":{"chicago":123,"boston":245}}' https://example.com
If this is actually how they expect data, it is a misconfigured server, as x-www-form-urlencoded
data should be in the form key=value
.
如果这实际上是他们期望数据的方式,则它是配置错误的服务器,因为x-www-form-urlencoded
数据应采用key=value
.