I Ate Some Delicious HTTPie

Tasteful interactions with HTTP endpoints.

Tagged with: tools

Published on and last updated on

Everyone who works with websites and web APIs at one point will have to test or debug some HTTP calls. A vanilla HTTP GET request can be done with your browser of choice by entering the URL into the address bar. But what do you do if you want to make a call with another HTTP verb or set some headers? There are many graphical applications like Insomnia or Postman and the almighty cURL. I have used all three of them to work with HTTP endpoints and they do a great job but there is a new kid in town.

HTTPie is a command-line tool that can be used to interact with HTTP endpoints. It provides features like colorized output, JSON support, and intuitive usability.

> http httpbin.org/status/418
HTTP/1.1 418 I'M A TEAPOT
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 135
Date: Sun, 15 Mar 2020 17:36:47 GMT
Server: gunicorn/19.9.0
x-more-info: http://tools.ietf.org/html/rfc2324


    -=[ teapot ]=-

       _...._
     .'  _ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`

The example above makes an HTTP GET request to http://httpbin.org/status/418 and prints the headers and body of the response. In the terminal, the headers are syntax highlighted. It looks similar to the following example.

HTTP/1.1 418 I'M A TEAPOT
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 135
Date: Sun, 15 Mar 2020 17:36:47 GMT
Server: gunicorn/19.9.0
x-more-info: http://tools.ietf.org/html/rfc2324

It is also possible to set headers for your request by writing them in the format Header:Value after the URL. In the following example, I set the User-Agent header for the request to Ichiban. The --body flag instructs HTTPie to only print the response body.

> http --body httpbin.org/headers User-Agent:Ichiban
{
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Host": "httpbin.org",
        "User-Agent": "Ichiban",
        "X-Amzn-Trace-Id": "Root=1-5e6e6be0-f16095947012ee8d94f75fe9"
    }
}

If another HTTP method then GET should be used the name of the method has to be written before the URL. Like in the following example where a DELETE call is made to http://httpbin.org/anything/42.

> http --body DELETE httpbin.org/anything/42
{
    "args": {},
    "data": "",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Content-Length": "0",
        "Host": "httpbin.org",
        "User-Agent": "HTTPie/0.9.8",
        "X-Amzn-Trace-Id": "Root=1-5e6e6e0d-fb2d984b569fadc4f6fddc92"
    },
    "json": null,
    "method": "DELETE",
    "origin": "91.115.48.164",
    "url": "http://httpbin.org/anything/42"
}

In my opinion, this tool doesn’t replace the other applications that I mentioned above. But when you quickly want to test if a call is working this is the right tool for the job. It is cross-platform, quick, and easy to use.