If you find cURL’s syntax a bit too dry for your liking, you could give HTTPie a try. In this article I will go through the basics of making HTTP requests to a REST API using HTTPie.

Install HTTPie

I am on OSX and with brew, installation is a breeze.

$ brew install httpie

If you are on a different system, check out the installation options

Talk to a REST API

For these examples, I will use the test API @ http://jsonplaceholder.typicode.com/

GET Request

Use http [url] for a basic GET request

$ http jsonplaceholder.typicode.com/posts/1

A nice feature of HTTPie is the possibility to use a name==value syntax for your querystring parameters.

Instead of the less readable:

$ http jsonplaceholder.typicode.com/comments?postId=1

Use the cleaner:

$ http jsonplaceholder.typicode.com/comments postId==1

POST Request

For other verbs than the default GET, use http [verb] [url] [data].

The [data] part of the command uses a white space separated list of key value pairs as defined below:

  • name=value for strings or text.
  • name:=value for raw JSON.
  • name=@file.txt to include the content of a text files sent as a string.
  • name:=@data.json to include a JSON file used as raw JSON.

Note on raw JSON:

You need to use the raw JSON name:=value notation for booleans and numbers for example. Otherwise bool=false will be interpreted as {bool: 'false'} instead of {bool: false}. Similarly, id=10 would be interpreted as {id: '10'} instead of {id: 10}.

Example

With our test API, to create a new post, we need to send a POST request to http://jsonplaceholder.typicode.com/posts, with the following JSON data:

{
title: 'foo',
body: 'bar',
userId: 1
}

The HTTPie request will be

$ http POST jsonplaceholder.typicode.com/posts title=foo body=bar userId:=1

PUT Request

PUT Requests work the same way as POST Request:

A PUT Request to http://jsonplaceholder.typicode.com/posts/1

With the data

{
id: 1,
title: 'foo',
body: 'bar',
userId: 1
}

becomes

$ http PUT jsonplaceholder.typicode.com/posts/1 id:=1 title=foo body=bar userId:=1

DELETE Request

Our DELETE Request does not have any data sent:

$ http DELETE jsonplaceholder.typicode.com/posts/1

Check out the excellent documentation for more HTTPie examples.