Thursday, February 13, 2014

Notes on cURL

9 uses for cURL worth knowing
http://httpkit.com/resources/HTTP-from-the-Command-Line/
Set the Request Method -X POST
The curl default HTTP method, GET, can be set to any method you would like using the -X option. The usual suspects POST, PUT, DELETE, and even custom methods, can be specified.
curl -X PUT \
     -H 'Content-Type: application/json' \
     -d '{"firstName":"Kris", "lastName":"Jordan"}'
     echo.httpkit.com

Set Request Headers -h
curl -H "Authorization: OAuth 2c4419d1aabeec" http://echo.httpkit.com
curl -H "Accept: application/json" -H "Authorization: OAuth 2c3455d1aeffc" http://echo.httpkit.com
Send a Request Body -d
curl -X PUT -H 'Content-Type: application/json' -d '{"firstName":"Kris", "lastName":"Jordan"}' echo.httpkit.com
Use a File as a Request Body @example.json
Escaping JSON/XML at the command line can be a pain and sometimes the body payloads are large files. Luckily, cURL’s @readfile macro makes it easy to read in the contents of a file.
curl -X PUT -H 'Content-Type: application/json' -d @example.json echo.httpkit.com
POST HTML Form Data -d
-d "firstName=Kris" -d "lastName=Jordan" \
POST HTML Multipart / File Forms
As you know from writing HTML file upload form, these use a multipart/form-data Content-Type, with the enctype attribute in HTML. In cURL we can pair the -F option and the @readFile macro covered above.
-F "firstName=Kris" -F "publicKey=@idrsa.pub;type=text/plain"

Send Post to Solr Query Handler
curl -X POST -F "q=query" -F "start=0" -F "rows=10" http://localhost:11111/solr/select

View Response Headers
curl -i echo.httpkit.com 

http://www.thegeekstuff.com/2012/04/curl-examples/
Save the cURL Output to a file -O
curl -O http://www.gnu.org/software/gettext/manual/gettext.html
curl -O http://www.gnu.org/software/gettext/manual/html_node/index.html -O http://www.gnu.org/software/gettext/manual/gettext.html

Follow HTTP Location Headers with -L option
curl -L http://www.google.com

Continue/Resume a Previous Download -C
curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html

Limit the Rate of Data Transfer --limit-rate 1000B
curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html
Download a file only if it is modified before/after the given time
curl -z 21-Dec-11 http://www.example.com/yy.html

Pass HTTP Authentication in cURL -u username:password
curl -u username:password URL

More Information using Verbose and Trace Option
curl -v http://google.co.in

Get Definition of a Word using DICT Protocol
curl dict://dict.org/d:bash


Download Files from FTP server
curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php
List/Download using Ranges
curl   ftp://ftp.uk.debian.org/debian/pool/main/[a-z]/

Upload Files to FTP Server
curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com
curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com
Optionally we can use “.” to get the input from STDIN and transfer to the remote.
$ curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt

No comments:

Post a Comment