The power of cURL for software tester

One of my favorite language for testing is cURL. As soon as I have to do with RESTful applications or simple HTTP, it’s my first choice. Here I show only a small selection of cURL options.

Basic examples:

Basic HTTP Get request

$ curl http://example.tld

Download`s

# Basic unix output redirection
$ curl http://example.tld > index.html

# Filename provided by command line
$ curl -o byname.html http://example.tld

# Filename provided by URL
$ curl -O http://example.tld/lorem/ipsum.html

# Multiple files
$ curl -O http://example.tld/lorem/ipsum.html -O http://example.tld/dolor/sit.html

Follow redirects

$ curl -L http://example.tld

HTTP referer

$ curl -e http://referer.com http://example.tld

Resumed transfer offset (continue download)

$ curl -C -O http://example.tld/lorem/ipsum.html

Show response header

$ curl -I http://example.tld

Change request method

# default is GET
$ curl http://example.tld

# force GET
$ curl -G http://example.tld

# use POST, PUT or DELETE
$ curl -X POST http://example.tld
$ curl -X PUT http://example.tld
$ curl -X DELETE http://example.tld

Set request headers

# JSON content type
$ curl -H "Content-Type: application/json" http://example.tld

# Accept-Language
$ curl -H "Accept-Language: de-DE" http://example.tld

Advanced examples:

Send a JSON request

$ curl -X PUT -H 'Content-Type: application/json' -d '{"firstName":"Max", "lastName":"Muster"}' http://example.tld

Use a file with JSON content

$ curl -X PUT -H 'Content-Type: application/json' -d @myfile.json http://example.tld

Specify HTTP multipart POST data (JSON and image)

$ curl -X POST \
-H 'Content-Type: multipart/form-data' \
-F "userData=@myfile.json;type=application/json" \
-F profilePicture=@image.jpg \
http://example.tld

Write output (show status and time total)

# Time total
$ curl -o /dev/null -s -w %{time_total}\\n  http://example.tld

# Status code and time total
$ curl -o /dev/null -s -w %{http_code}:%{time_total}\\n  http://example.tld

Simple server authentication

$ curl -u username:password http://example.tld

FTP

# FTP (root folder)
$ curl ftp://username:password@example.tld

# FTP (specific folder)
$ curl -u username:password ftp://example.tld/textfiles/

# FTP upload (specific folder)
$ curl -T myfile.txt -u username:password ftp://example.tld/textfiles/myfile.txt

# FTP delete (root folder)
$ curl -X 'DELE myfile.txt' -u username:password ftp://example.tld

Ignore SSL certificate error

$ curl -k https://example.tld

For more information see help and man pages!!!!