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:
-
Send multiple files in a single “field” with a single field name:
curl -F "pictures=@dog.gif,cat.gif" -
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.
备份地址: 【curl 命令提交post表单】