As we shift toward microservice oriented architectures, APIs are becoming more and more pervasive. While there are many tools that help us be more productive, none are as universal as curl. Before we dive into some of the common commands I use, here are other tools which are crucial to have in your toolbox.

Make a GET request

A GET request is the simplest kind of request you can do with curl. You only have to specify URL.

% curl https://httpbin.org/get
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "origin": "",
  "url": "https://httpbin.org/get"
}

Make a HEAD request

Sometimes you are only interested in the response headers. You can use the -I flag to specify a HEAD request.

% curl -I https://httpbin.org/headers
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 04 Nov 2016 21:54:59 GMT
Content-Type: application/json
Content-Length: 105
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

Follow the location

A lot of requests result in a 301 or 302 redirect, and sometimes there are several redirects before you reach the final destination. If you want to find the final destination you can use the HEAD request along with the -L flag to tell curl to follow the location.

% curl -IL https://httpbin.org/redirect/2
HTTP/1.1 302 FOUND
Server: nginx
Date: Fri, 04 Nov 2016 21:54:05 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 247
Connection: keep-alive
Location: /relative-redirect/1
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

HTTP/1.1 302 FOUND
Server: nginx
Date: Fri, 04 Nov 2016 21:54:05 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: /get
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 04 Nov 2016 21:54:05 GMT
Content-Type: application/json
Content-Length: 187
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

Make a POST request

You can make a POST request by using the -X flag along with the http verb you want. In this case, we want to use POST.

curl -X POST https://httpbin.org/post
{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": null,
  "origin": "",
  "url": "https://httpbin.org/post"
}

Make a POST request with data

When making POST (PUSH or PATCH) requests we will often want to set the body data. To do this, you can use the -d flag.

% curl -X POST -d "foo=bar" https://httpbin.org/post
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "foo": "bar"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "7",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": null,
  "origin": "",
  "url": "https://httpbin.org/post"
}

Make a POST request with headers

When sending a POST request, you will often need to set additional headers. For example, you may need to set the Content-Type header when the body of the request contains JSON. To do so, you can use the -H flag.

% curl -X POST -d '{"foo": "bar"}' -H "Content-Type: application/json" https://httpbin.org/post
{
  "args": {},
  "data": "{\"foo\": \"bar\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "14",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.43.0"
  },
  "json": {
    "foo": "bar"
  },
  "origin": "",
  "url": "https://httpbin.org/post"
}

Set BasicAuth credentials

There are two ways to set the credentials for basic auth. You can prepend the username and password domain or use the --user flag

% curl -I https://httpbin.org/basic-auth/root/password --user root:password
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 04 Nov 2016 22:09:39 GMT
Content-Type: application/json
Content-Length: 47
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

% curl -I https://root:password@httpbin.org/basic-auth/root/password
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 04 Nov 2016 22:10:07 GMT
Content-Type: application/json
Content-Length: 47
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

Measure Request Duration

Sometimes you want to measure the time it takes to receive a response from the server. For this we can use the -w option and specify a format. Since we are only interested in the time, we will also use the -s option to put curl in silent mode and the -o option to redirect the output.

% curl -s -o /dev/null -w "Total Time: %{time_total}\n" "https://httpbin.org/delay/3"
Total Time: 3.456
Author

Write A Comment