Difference between revisions of "Curl"

From Notes_Wiki
m
m
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
<yambe:breadcrumb self="Curl">Shell scripting|Shell scripting</yambe:breadcrumb>
[[Main_Page|Home]] > [[Shell scripting]] > [[Curl|curl]]
=curl=
 
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[Network related tools]] > [[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.
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.
Line 7: Line 8:
==Login into a web page using POST==
==Login into a web page using POST==
<pre>
<pre>
curl -c cookies -X POST --url 'https://.../login.php' -D -  --data-urlencode 'username=saurabh@rekallsoftware.com' --data-urlencode 'password=secret'
curl -c cookies -X POST --url 'https://.../login.php' -D -  --data-urlencode 'username=saurabh@sexample.com' --data-urlencode 'password=secret'
</pre>
</pre>
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.
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.
Line 17: Line 18:
;-D - : Will make curl print headers returned by server on stdout
;-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.
;--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.
There are also useful options:
;-H <header> : To give header such as 'Content-Type: application/json'
;-k : To ignore SSL certificate given by server (Self-signed)
;-u <username> : To give username with which to login.  The password would be prompted after we give the command.
; -d @<file> : To give input via file instead of making it part of command line.
; -m <time-in-seconds> : Maximum time to wait before timeout.  In case of batch scripts expected to finish soon this can be set lower.  In case of long API calls it can be set higher.  Default is approx 300 (300,000 milliseconds).
Refer:
* https://linuxize.com/post/curl-post-request/
* https://everything.curl.dev/usingcurl/timeouts




Line 51: Line 65:




<yambe:breadcrumb self="Curl">Shell scripting|Shell scripting</yambe:breadcrumb>
[[Main_Page|Home]] > [[Shell scripting]] > [[Curl|curl]]
 
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[Network related tools]] > [[Curl]]

Latest revision as of 13:08, 23 August 2022

Home > Shell scripting > curl

Home > CentOS > CentOS 6.x > Network related tools > 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@sexample.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.


There are also useful options:

-H <header>
To give header such as 'Content-Type: application/json'
-k
To ignore SSL certificate given by server (Self-signed)
-u <username>
To give username with which to login. The password would be prompted after we give the command.
-d @<file>
To give input via file instead of making it part of command line.
-m <time-in-seconds>
Maximum time to wait before timeout. In case of batch scripts expected to finish soon this can be set lower. In case of long API calls it can be set higher. Default is approx 300 (300,000 milliseconds).

Refer:


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.


Home > Shell scripting > curl

Home > CentOS > CentOS 6.x > Network related tools > Curl