Difference between revisions of "Curl"

From Notes_Wiki
m
Line 1: Line 1:
<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb>
<yambe:breadcrumb self="Curl">Network related tools|Network related tools</yambe:breadcrumb>
<yambe:breadcrumb>Network related tools</yambe:breadcrumb>
<yambe:breadcrumb>Network related tools</yambe:breadcrumb>
=curl=
=curl=
Line 53: Line 53:


<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb>
<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb>
<yambe:breadcrumb>Network related tools</yambe:breadcrumb>
<yambe:breadcrumb self="Curl">Network related tools|Network related tools</yambe:breadcrumb>

Revision as of 08:45, 9 September 2018

<yambe:breadcrumb self="Curl">Network related tools|Network related tools</yambe:breadcrumb> <yambe:breadcrumb>Network related tools</yambe:breadcrumb>

curl

curl is very versatile tools for web requests both for normal HTTP requests with POST payload (form values or even file attachments) and also for REST API usage. Few example curl commands are mentioned below to showcase usability of curl.


Login into a web page using POST

curl -c cookies -X POST --url 'https://.../login.php' -D -  --data-urlencode 'username=saurabh@rekallsoftware.com' --data-urlencode 'password=secret'

This will make a POST login request to login.php file on chosen server. Both username and password will be properly urlencoded by curl. Note that the field names 'username' and 'password' are supposed to be URL encoded already.

Here:

-c cookies
Ensures that cookies returned by server are stored in file named cookies for future use
-X POST
Makes a POST request
--url 'https://.../loging.php'
Points to URL where HTTP POST request should be sent
-D -
Will make curl print headers returned by server on stdout
--data-urlencode 'variable=value'
Can be used to submit various post values. Value will get URL encoded by curl. Variable must be urlencoded by caller.


Make a GET request using exiting cookies

curl -b cookies -X GET --url 'https://.../welcome.php' -D -

This will make a GET request to give page using cookies obtained in some previous step, such as login.

Here:

-b cookies
Supply cookies stored in cookie jar file while making current request to maintain session information. Note that new cookie values will not automatically get added to same file. If that is desired then -c coookies should also be specified on command-line


Make a DELETE request

curl -b cookies -X DELETE --url 'https://.../api/delete.php?id=c17c1d55-26a1-402d-94a8-16cf479d107b' -D -

This will make a DELETE request to given page using cookies obtained in some previous step, such as login.

Here:

-X DELETE
This causes the request method to be DELETE. Any method can be specified and values are not limited to GET, POST, HEAD or OPTIONS.


Downloading files

curl -c cookies -X GET --url 'http://.../captcha_code_file.php?rand=920936120' -o captcha.jpg

Here:

-o captcha.jpg
This is used to store the HTTP response data into file named captcha.jpg for future use. Cookies returned by server are stored separately in cookies file.


<yambe:breadcrumb>Shell_scripting|Shell scripting</yambe:breadcrumb> <yambe:breadcrumb self="Curl">Network related tools|Network related tools</yambe:breadcrumb>