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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:01:20  来源:igfitidea点击:

cURL send JSON as x-www-form-urlencoded

bashcurl

提问by Kousha

I want to post the following JSON:

我想发布以下 JSON:

{
   "cities": {
       "chicago": 123,
       "boston": 245
   }
}

Using curlas x-www-form-urlencodedwithout using a .json file. I cannot figure out how to build the curl -F ...

使用curlasx-www-form-urlencoded而不使用 .json 文件。我不知道如何构建curl -F ...

回答by ikos23

For application/x-www-form-urlencodedyou could try:

因为application/x-www-form-urlencoded你可以尝试:

curl -d "param1=value1&param2=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&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/blahblah

Instead of http://localhost:3000/blahblahyou 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 -dto 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-urlencodeddata should be in the form key=value.

如果这实际上是他们期望数据的方式,则它是配置错误的服务器,因为x-www-form-urlencoded数据应采用key=value.