curl 命令提交post表单

POST (HTTP)

It’s easy to post data using curl. This is done using the -d
option. The post data must be urlencoded.

Post a simple “name” and “phone” guestbook.

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

How to post a form with curl, lesson #1:

Dig out all the tags in the form that you want to fill in. (There’s
a perl program called formfind.pl on the curl site that helps with this).

If there’s a “normal” post, you use -d to post. -d takes a full “post
string”, which is in the format

    <variable1>=<data1>&<variable2>=<data2>&...

The ‘variable’ names are the names set with “name=” in the tags, and
the data is the contents you want to fill in for the inputs. The data must
be properly URL encoded. That means you replace space with + and that you
replace weird letters with %XX where XX is the hexadecimal representation of
the letter’s ASCII code.

Example:

(page located at http://www.formpost.com/getthis/

    <form action="post.cgi" method="post">
    <input name=user size=10>
    <input name=pass type=password size=10>
    <input name=id type=hidden value="blablabla">
    <input name=ding value="submit">
    </form>

We want to enter user ‘foobar’ with password ‘12345’.

To post to this, you enter a curl command line like:

    curl -d "user=foobar&pass=12345&id=blablabla&ding=submit"  (continues)
      http://www.formpost.com/getthis/post.cgi

While -d uses the application/x-www-form-urlencoded mime-type, generally
understood by CGI’s and similar, curl also supports the more capable
multipart/form-data type. This latter type supports things like file upload.

-F accepts parameters like -F “name=contents”. If you want the contents to
be read from a file, use <@filename> as contents. When specifying a file,
you can also specify the file content type by appending ‘;type=’
to the file name. You can also post the contents of several files in one
field. For example, the field name ‘coolfiles’ is used to send three files,
with different content types using the following syntax:

    curl -F "coolfiles=@fil1.gif;type=image/gif,fil2.txt,fil3.html" \
    http://www.post.com/postit.cgi

If the content-type is not specified, curl will try to guess from the file
extension (it only knows a few), or use the previously specified type (from
an earlier file if several files are specified in a list) or else it will
use the default type ‘application/octet-stream’.

Emulate a fill-in form with -F. Let’s say you fill in three fields in a
form. One field is a file name which to post, one field is your name and one
field is a file description. We want to post the file we have written named
“cooltext.txt”. To let curl do the posting of this data instead of your
favourite browser, you have to read the HTML source of the form page and
find the names of the input fields. In our example, the input field names
are ‘file’, ‘yourname’ and ‘filedescription’.

    curl -F "file=@cooltext.txt" -F "yourname=Daniel" \
         -F "filedescription=Cool text file with cool text inside" \
         http://www.post.com/postit.cgi

To send two files in one post you can do it in two ways:

  1. Send multiple files in a single “field” with a single field name:

    curl -F "pictures=@dog.gif,cat.gif"
    
  2. Send two fields with two field names:

    curl -F "docpicture=@dog.gif" -F "catpicture=@cat.gif"
    

    To send a field value literally without interpreting a leading ‘@’
    or ‘<’, or an embedded ‘;type=’, use –form-string instead of
    -F. This is recommended when the value is obtained from a user or
    some other unpredictable source. Under these circumstances, using
    -F instead of –form-string would allow a user to trick curl into
    uploading a file.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少言才不会咸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值