Curl in html forms : Use of curl
Curl in html forms : Use of curl
Forms are the general way for the user to enter data in, and then press some kind of ‘OK’ or ‘Submit’ button to get that data sent to the server.
The server then typically uses the posted data to decide how to act. Like using the entered words to search in a database, or to add the info in a bug tracking system, display the entered address on a map or using the info as a login-prompt verifying that the user is allowed to see what it is about to see.
Of course there has to be some kind of program on the server end to receive the data you send.
GET request with CURL
A GET-form uses the method GET, as specified in HTML like:
<form method=”GET” action=”testapp.cgi”>
<input type=text name=”dob”>
<input type=submit name=press value=”OK”>
</form>
Now once you submit, the page will go with new url like this:
“testapp.cgi?dob=2018&press=OK”
We can use the curl as this way:
curl “http://www.blog.eduguru.in/test/testapp.cgi?dob=2018&press=OK”
POST request with CURL
The GET method makes all input field names get displayed in the URL field of your browser.
That’s generally a good thing when you want to be able to bookmark that page with your given data, but it is an obvious disadvantage if you entered secret information in one of the fields or if there are a large amount of fields creating a very long and unreadable URL.
The HTTP protocol then offers the POST method. This way the client sends the data separated from the URL and thus you won’t see any of it in the URL address field.
The POST form looks similar to GET from with only difference in form method. for POST, we need to write POST.
<form method=”POST” action=”testapp.cgi”> <input type=text name=”dob”> <input type=submit name=press value=” OK “> </form>
Now using CURL, we can POST the data as:
And to use curl to post this form with the same data filled in as before, we could do it like:
curl –data “dob=2018&press=OK” http://blog.eduguru.in/testapp.cgi
This kind of POST will use the Content-Type application/x-www-form-urlencoded and this is the most widely used POST kind.
The data being send to the server must be properly encoded, curl will not do that for you.
For example, if you want the data to contain a space, you need to replace that space with %20 etc. Failing to comply with this will most likely cause your data to be received wrongly and messed up.
However new version of curl can in fact url-encode POST data for you as:
curl –data-urlencode “testfield=Hello, How are you” http://blog.eduguru.in