有田地。
curl --data "param1=value1¶m2=value2" https://example.com/resource.cgi
有单独指定的字段。
curl --data "param1=value1" --data "param2=value2" https://example.com/resource.cgi
多部分。
curl --form "[email protected]" https://example.com/resource.cgi
带有字段和文件名的多部件。
curl --form "[email protected];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
更多信息请参见【cURL手册】1。cURL模拟网络浏览器的教程]2很有帮助。
在libcurl中,使用curl_formadd()
函数来构建你的表单,然后再以常规方式提交。 更多信息见libcurl文档。
对于大文件,考虑添加参数来显示上传进度。
curl --tr-encoding -X POST -v -# -o output -T filename.dat \
http://example.com/resource.cgi
-o输出
是必须的,否则不会出现进度条。
对于一个包含XML的RESTful HTTP POST。
gt;
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 "的文件内容,并作为post请求发送。
来自stdin的数据与-d @-
。
例如:
echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown
。
echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown
产出:{{{10800384}}
<p>Hello <strong>world</strong>!</p>
curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi
是在Curl实例手册中找到的例子。
如果上面的方法不奏效,请使用%26来表示安培数。
curl -d "name=Rafael%20Sagula%26phone=3320780" http://www.where.com/guest.cgi
如果您想登录一个网站,请执行以下操作。
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/
第一个请求会在"headers".文件中保存会话cookie(成功登录时提供)。 文件中。 从现在起,您可以使用该cookie来验证您使用浏览器登录后通常访问的网站的任何部分。
curl -v --data-ascii var=value http://example.com
还有很多选项,请查看curl --help
以获得更多信息。
如果你很懒,你可以让google-chrome为你做所有的工作。
选择devtools中的网络选项卡,勾选保存日志复选框。
提交表单并找到方法为POST的条目(右键单击任何列标题并确保方法被选中)。
右键单击带有POST的行,选择Copy >。 5.复制为cURL。
[![chrome devtools: 复制为cURL][1]][1] 。
Chrome会用cURL语法复制所有的请求数据。
Chrome使用--data 'param1=hello¶m2=world'
,你可以根据你想发送的POST请求类型,在每个参数中使用一个-d
或-F
来使其更易读,它可以是application/x-www-form-urlencoded
或multipart/form-data
。
这将以application/x-www-form-urlencoded
的形式被POST-ed(用于大多数不包含文件上传的表单)。
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'头通常不需要,但我把它加入进来以备不时之需。 你可以通过创建
~/.curlrc文件来避免在每个请求中设置用户代理,该文件包含如下内容
User-Agent:
"Mozilla/2.2"`