cURL 将 JSON 作为 x-www-form-urlencoded 发送

Kou*_*sha 18 bash curl

我想发布以下 JSON:

{
   "cities": {
       "chicago": 123,
       "boston": 245
   }
}
Run Code Online (Sandbox Code Playgroud)

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

joh*_*ohn 43

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

curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/blahblah
Run Code Online (Sandbox Code Playgroud)

哪里param1=value...必须是你的 JSON 数据chicago=123&boston=245

或显式形式:

curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/blahblah
Run Code Online (Sandbox Code Playgroud)

而不是http://localhost:3000/blahblah您应该提供您的服务的真实 URL。


mik*_*n32 7

根据手册页,整个要点curl -F是“根据 RFC 2388 使用 Content-Type multipart/form-data POST 数据”。换句话说,当您需要使用文件输入模拟 HTML 表单时,最好使用它。

而是使用curl -d指定原始 POST 数据:

curl -d '{"cities":{"chicago":123,"boston":245}}' https://example.com
Run Code Online (Sandbox Code Playgroud)

如果这实际上是他们期望数据的方式,那么它是一个配置错误的服务器,因为x-www-form-urlencoded数据应该采用以下形式key=value