【问题标题】:How do I make a POST request using curl?执行 POST 请求的 cURL 命令行语法是什么?
【发布时间】:2008-09-17 15:39:01
【问题描述】:

如何使用cURL 命令行工具发出POST 请求?

【问题讨论】:

    标签: http curl


    【解决方案1】:

    带字段:

    curl --data "param1=value1&param2=value2" https://example.com/resource.cgi
    

    单独指定字段:

    curl --data "param1=value1" --data "param2=value2" https://example.com/resource.cgi
    

    多部分:

    curl --form "fileupload=@my-file.txt" https://example.com/resource.cgi
    

    包含字段和文件名的多部分:

    curl --form "fileupload=@my-file.txt;filename=desired-filename.txt" --form param1=value1 --form param2=value2 https://example.com/resource.cgi
    

    没有数据:

    curl --data '' https://example.com/resource.cgi
        
    curl -X POST https://example.com/resource.cgi
    
    curl --request POST https://example.com/resource.cgi
    
    

    有关详细信息,请参阅the cURL manualcURL tutorial on emulating a web browser 很有帮助。

    使用libcurl,使用curl_formadd() 函数在以通常方式提交之前构建您的表单。请参阅libcurl documentation 了解更多信息。

    对于大文件,考虑添加参数以显示上传进度:

    curl --tr-encoding -X POST -v -# -o output -T filename.dat \
          http://example.com/resource.cgi
    

    -o output是必填项,否则不会出现进度条。

    【讨论】:

    • @LauriRanta --data-urlencode(没有破折号),至少在最近的版本中
    • 如果您需要使用 PUT 更新资源也可以使用: curl -X PUT ...
    • 我无法理解......我什么时候做With Fields,什么时候做Multipart,什么时候做Without Data
    • 你可以用--data代替-d
    • 我有一个字段数组。我该怎么做?
    【解决方案2】:

    对于包含 XML 的 RESTful HTTP POST:

    curl -X POST -d @filename.txt http://example.com/path/to/resource --header "Content-Type:text/xml"

    或者对于 JSON,使用这个:

    curl -X POST -d @filename.txt http://example.com/path/to/resource --header "Content-Type:application/json"

    这将读取名为filename.txt 的文件的内容并将其作为发布请求发送。

    【讨论】:

    • @tom-wijsman 解释:curl -X POST 表示 HTTP POST 请求,-d 参数(长版本:--data)告诉 curl 后面的将是 POST 参数,@filename将文件filename 的内容指定为参数。这种方法最适用于 RESTful HTTP API,如 Twitter、Facebook、包括 Ruby on Rails 在内的各种其他 Web 服务以及 CouchDB 等数据库的 HTTP API。 REST 代表Representational state transfer
    • 我们如何才能看到响应 xml 不是一行而是格式化的?
    • 我认为您可以省略-X POST,因为-d 暗示了这一点。
    • 如何给多个表头?
    • 多个标题: curl -H "header2:1" -H "header2:2" ...
    【解决方案3】:

    来自标准输入的数据,-d @-

    例子:

    echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown
    

    输出:

    <p>Hello <strong>world</strong>!</p>
    

    【讨论】:

    • 如果剪贴板中已有 JSON 对象,那就太好了
    • 更好:echo "$message" | curl -H "Content-Type: application/json" -d @- "$url"
    【解决方案4】:
    curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi 
    

    是在Curl Example Manual 中找到的示例。

    如果上述方法不起作用,请使用 %26 作为 & 符号:

    curl -d "name=Rafael%20Sagula%26phone=3320780" http://www.where.com/guest.cgi 
    

    【讨论】:

      【解决方案5】:

      如果您想登录网站,请执行以下操作:

      curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
      curl -L -b headers http://localhost/
      

      第一个请求将会话 cookie(在成功登录时提供)保存在“headers”文件中。从现在开始,您可以使用该 cookie 对您在使用浏览器登录后通常访问的网站的任何部分进行身份验证。

      【讨论】:

      • 来自 curl 手册页的注释:'-c, --cookie-jar 选项是存储 cookie 的更好方法。'
      【解决方案6】:

      如果你懒惰,你可以让 google-chrome 或 firefox 为你完成所有工作。

      1. 右键单击您要提交的表单并选择Inspect(或 Inspect Element(对于 Firefox)。这将打开 DevTools 面板。
      2. 选择 devtools 中的 Network 标签并勾选 Preserve log 复选框(Persist Logs for firefox)。
      3. 提交表单并使用 POST 方法找到条目(右键单击任何列标题并确保选中 Method)。
      4. 右键单击带有 POST 的行,然后选择复制 > 复制为 cURL

      Chrome 将以 cURL 语法复制所有请求数据。

      Chrome 使用 --data 'param1=hello&amp;param2=world',您可以通过使用单个 -d-F 每个参数来提高可读性,具体取决于您要发送的 POST 请求类型,可以是 application/x-www-form-urlencodedmultipart/form-data相应地。

      这将被发布为application/x-www-form-urlencoded用于大多数不包含文件上传的表单):

      curl http://httpbin.org/post \
          -H "User-Agent: Mozilla/2.2" \
          -d param1=hello \
          -d name=dinsdale
      

      对于multipart/form-data POST,使用-F通常用于包含文件上传的表单,或者字段顺序很重要,或者需要多个同名字段的情况):

      curl http://httpbin.org/post \
          -H "User-Agent: Mozilla/2.2" \
          -F param1=hello \
          -F name=dinsdale \
          -F name=piranha
      

      User-Agent 标头通常不需要,但我已将其丢弃以防万一。如果您需要自定义代理,那么您可以通过创建包含例如User-Agent: "Mozilla/2.2"

      【讨论】:

        【解决方案7】:
        curl -v --data-ascii var=value http://example.com
        

        还有更多选项,请查看curl --help 了解更多信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-01
          • 2011-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-23
          • 1970-01-01
          相关资源
          最近更新 更多