【问题标题】:Curl POST request in JSON format以 JSON 格式卷曲 POST 请求
【发布时间】:2014-08-03 13:30:00
【问题描述】:

我想在 mac 终端中使用 Curl 将 JSON 格式的 HTTP POST 请求发送到特定的 URL。

  1. 如何指定 HTTP 动词 POST? -d 和 -X 有什么区别?
  2. 如何指定以 JSON 格式发送数据?
  3. 关于如何测试请求本身有什么建议吗?在执行“实时”请求之前,我想测试并确切了解正在发送的 JSON 数据。我可以在本地主机上运行 Rails 服务器并将我的 POST 请求发送到本地主机吗?如何查看 JSON 数据?

欢迎提供任何示例。 谢谢!

【问题讨论】:

    标签: ruby-on-rails json http post curl


    【解决方案1】:

    1) 如果您使用-d 选项上传数据,curl 会自动使用POST。当您想要指定方法(PUTDELETE 等)而不是让 curl 为您选择时,使用 -X 选项。

    echo "how are you" | curl -vvv -d@- http://localhost:8000
    * About to connect() to localhost port 8000 (#0)
    *   Trying 127.0.0.1...
    * connected
    * Connected to localhost (127.0.0.1) port 8000 (#0)
    > POST / HTTP/1.1
    > User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
    > Host: localhost:8000
    > Accept: */*
    > Content-Length: 11
    > Content-Type: application/x-www-form-urlencoded
    > 
    * upload completely sent off: 11 out of 11 bytes
    < HTTP/1.1 200 OK
    < Date: Sun, 03 Aug 2014 13:46:44 GMT
    < Connection: keep-alive
    < Transfer-Encoding: chunked
    

    2) 您可以使用Content-type 标头指定您以json 格式发送数据。可以使用 -H 选项在 curl 中添加此标头。

    3) 是的,您可以设置一个网络服务器(使用 python, nodejs, rails 等),它可以在收到 http body 后打印出来。

    【讨论】:

      【解决方案2】:
      1. $ curl -d "param1=value1&amp;param2=value2" http://example.com/posts
      2. $ curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://example.com/posts
      3. 我更喜欢使用https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

      【讨论】:

        猜你喜欢
        • 2018-09-20
        • 2019-03-31
        • 1970-01-01
        • 2020-11-10
        • 2017-04-13
        • 2018-07-08
        • 1970-01-01
        • 2016-08-12
        • 2011-05-15
        相关资源
        最近更新 更多