【问题标题】:How to make a POST call with JSON data using curl?如何使用 curl 使用 JSON 数据进行 POST 调用?
【发布时间】:2016-10-21 12:42:31
【问题描述】:

我只尝试了使用 curl 的 GET 调用,现在我需要进行 POST 调用。我需要使用 JSON 提供参数或输入。这个命令怎么写?

【问题讨论】:

    标签: json api curl post


    【解决方案1】:

    假设您询问如何使用 JSON 属性格式化 curl POST 请求。

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{'json':{'key':'value'}}" http://your.domain/endpoint/here
    

    This page 涵盖了很多有用的案例。

    【讨论】:

    • 感谢 Jack 和 Hrishikesh,您的回答对我帮助很大。我形成了以下 curl 命令: curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{'json':{"Name": "web", "TaskTemplate":{"ContainerSpec":{"Image":"nginx:alpine","Mounts":[{"VolumeOptions":{"DriverConfig":{}}}],"User":"33"}, "LogDriver":{"Name":"json-file","Options":{"max-file":"3","max-size":"10M"}}}}}" https://:2376/services/create 。但它给了我 500 错误,你能回顾一下我犯的错误,这对我有很大帮助
    • 您的 API 可能不希望您的 json 以名为 json 的键开头,因此请删除它。 {“名称”:“web”,“TaskTemplate”:{“ContainerSpec”:{“Image”:“nginx:alpine”,“Mounts”:[{“VolumeOptions”:{“DriverConfig”:{}}}], “用户”:“33”},“LogDriver”:{“名称”:“json-file”,“选项”:{“max-file”:“3”,“max-size”:“10M”}} }}
    • 我试过那个千斤顶,但没有解决问题。以下是我收到的错误消息:HTTP/1.1 500 Internal Server Error Content-Type: application/json Server: Docker/1.12.2 (linux) Date: Fri, 21 Oct 2016 14:12:12 GMT Content-Length: 81
    • 在这个问题的背景下几乎不可能诊断出来。我建议提出一个新问题,包括服务器上正在运行的应用程序以及应用程序日志。可能它无法处理给定输入中的某些内容
    【解决方案2】:

    您需要在命令中使用用户标题选项,如下所示。
    -H/--header "Content-Type: application/json"

    所以完整的命令如下: curl -H "Content-Type: application/json" -X POST -d '{"username":"abc","password":"xyz"}' http://url/to/some/server

    【讨论】:

    • 我试过你的格式Hrishikesh。它对我来说按预期工作,非常感谢。
    • 我尝试在 Linux 上使用相同的格式,但出现“未提供数据”错误。
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 2014-03-09
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多