Curl simulates form post submission and use of related common parameters (including submitting a form to upload files simultaneously)

Source: Internet
Author: User
Tags form post

Reprinted from: 432,781,571. First, the simplest case is that we just need to submit a form without a file upload, in which case we just need to use--data (note--not-) or its abbreviation-D in Curl. [Plain]View PlainCopy
    1. Curl-d "key=value&key=value" "url"
Or [Plain]View PlainCopy
    1. Curl--data "key=value&key=value" "url"
(Note: If there is only one key-value pair, you can not write double quotes, but if there are multiple key-value pairs, you must add double quotes, key-value pairs with & connection) Now suppose we have a landing form, it might look like this [HTML]View PlainCopy
  1. <form action="Dologin" method="POST">
  2. <input type="text" name="username" value="admin"/>
  3. <input type="password" name="password" value="admin"/>
  4. <button type="Submit" value= "submit" />
  5. </form>
Then our curl should be written like this. [Plain]View PlainCopy
    1. Curl-d "Username=admin&password=admin" "Www.xxxx.com/doLogin"
If the page is redirected after landing, and we want to track the redirect, you can add the-l parameter (L must capitalize) to indicate that the trace redirect [Plain]View PlainCopy
    1. Curl-l-D "username=admin&password=admin" "Www.xxx.com/doLogin"
Two. A slightly more complicated situation: now we want to make some post requests that must be logged on, then we need to save our previous successful login cookie information before we carry the login information for POST request 1. Saving the login cookie information to the file can be implemented by any one of the following 2 options-C: Save cookie Information-D: Save the entire header information, including examples of cookies: [Plain]View PlainCopy
    1. Curl-c cookie.txt-d "Username=admin&password=admin" "Www.xxx.com/doLogin"
After the run will be in the directory where you execute the instructions to generate a Cookie.txt file, of course you can also specify the file creation path, relative path absolute path can be (such as cookie.txt can be changed to/path/cookie.txt) 2. Carrying login information for post requests requires the-B option-B: Specifies which file example to use: [Plain]View PlainCopy
    1. Curl-l-B cookie.txt-d "key=value" "url"
Three. Simulation form file upload, equivalent to the form form method= "POST" and enctype= "multipart/form-data" Situation this time need to use the F option curl-f "[Email Protected] "url" false currently our file upload form is like this: [HTML]View PlainCopy
  1. <form action="upload" method="POST" enctype="Multipart/form-data">
  2. <input type="file" name="myfile" />
  3. <button type="Submit" value= "submit" />
  4. </form>
We want to submit a file named Hellocurl.zip, which is in the root directory where our instructions are located. So that's what our curl is. [Plain]View PlainCopy
    1. Curl-f "[Email protected]" "Www.xxx.com/upload"
If you upload more than one file, you can write multiple-f "[email protected]" [Plain]View PlainCopy
    1. Curl-f "[Email protected]"-F "[email protected]" Www.xxx.com/upload "
Take a closer look at a more realistic scenario: many times, our forms also contain many other form elements, such as text,checkbox,select and so on. Now let's add more elements to the form above, and change to the following: [HTML]View PlainCopy
  1. <form  action=< Span class= "Attribute-value" > "upload"  method= "POST"  enctype= "Multipart/form-data" >  
  2.     <span  Style= "color: #990000;" ><input type=< Span class= "Attribute-value" > "text"   name= " FileName "  value=" Hellocurl " /></span>  
  3. <input type="file" name="myfile" />
  4. <button type="Submit" value= "submit" />
  5. </form>
At the same time we would like to submit Hellocurl.zip file This time we might think of adding-D special attention, at this point we can not use both-D and-F, these 2 options in Curl is a two-way request,-D is application/x-www-url- The encoded method sends the POST request, and the-F is multipart/form-data, and if the-D and-f options are used simultaneously in curl, the warning:you can only select one HTTP request! Warning, which causes the instruction to fail to execute in this case, many of the solutions given online now are solved by adding-F to the solution, namely: [Plain]View PlainCopy
    1. Curl-f "Filename=hellocurl"-F "[email protected]" "Www.xxx.com/upload"
But when I tested this method in a Spring MVC project, I found that because I couldn't get the value of the filename, I couldn't bypass the server-side validation, causing the commit to fail. If you encounter a similar situation, try the following workaround: [Plain]View PlainCopy
    1. Curl-f "[Email protected]" "Www.xxx.com/upload? Key1=value1&key2=value2 "
Specific to the current problem our curl is: [Plain]View PlainCopy
    1. Curl-f "[Email protected]" "Www.xxx.com/upload? Filename=hellocurl "
The method is passed by the pro-test and is still the post submission method. Some other practical options:-V: View the entire complete process of the request-I: View the request header information-O: Save the response result to a file of the specified path, such as: Curl-o tmp.txt http://www.csdn.net #保存响应 To Tmp.txt

Curl simulates form post submission and use of related common parameters (including submitting a form to upload files simultaneously)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.