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
- Curl-d "key=value&key=value" "url"
Or
[Plain]View PlainCopy
- 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
- <form action="Dologin" method="POST">
- <input type="text" name="username" value="admin"/>
- <input type="password" name="password" value="admin"/>
- <button type="Submit" value= "submit" />
- </form>
Then our curl should be written like this.
[Plain]View PlainCopy
- 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
- 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
- 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
- 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
- <form action="upload" method="POST" enctype="Multipart/form-data">
- <input type="file" name="myfile" />
- <button type="Submit" value= "submit" />
- </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
- 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
- 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
- <form action=< Span class= "Attribute-value" > "upload" method= "POST" enctype= "Multipart/form-data" >
- <span Style= "color: #990000;" ><input type=< Span class= "Attribute-value" > "text" name= " FileName " value=" Hellocurl " /></span>
- <input type="file" name="myfile" />
- <button type="Submit" value= "submit" />
- </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
- 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
- Curl-f "[Email protected]" "Www.xxx.com/upload? Key1=value1&key2=value2 "
Specific to the current problem our curl is:
[Plain]View PlainCopy
- 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)