Skip to content

Instantly share code, notes, and snippets.

@vnavarro
Last active February 3, 2016 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vnavarro/3a456ec75322b6b39c30 to your computer and use it in GitHub Desktop.
Save vnavarro/3a456ec75322b6b39c30 to your computer and use it in GitHub Desktop.
Using http clients: cURL, HTTPie, RESTY

Here you find a fast how to test of an API with cURL, HTTPie and RESTY. The API is simulated using JSON Server tool from typicode.

Install with npm npm install -g json-server

We use typicode sample json

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ]
}

After creating db.json run json-server --watch db.json

GET

curl http://localhost:3000/posts
curl -X GET http://localhost:3000/comments/1
curl -H "Content-type: application/json" -X GET http://localhost:3000/posts/1/comments

POST

Add new post

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"author": "vnavarro", "id":2, "title":"post from curl"}' http://localhost:3000/posts

Add new comment

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"body": "another comment", "id":2, "postId":1}' http://localhost:3000/comments

PUT/PATCH

Update post with id:1

curl -H "Accept: application/json" -H "Content-type: application/json" -X PATCH -d '{"author": "vnavarro", "id":1}' http://localhost:3000/posts/1

Check this for more info on PUT and PATCH

DELETE

Remove comment with id:2 curl -X DELETE http://localhost:3000/comments/2

Parameters meaning

  • -H add header
  • -X specify HTTP Method
  • -d specify data (e.g. form submit value, JSON, file)

Install with brew install httpie

GET

http http://localhost:3000/posts
http :3000/posts

For localhost we can use the short :3000/posts or just /posts when localhost is using port 80.

POST

Add new post

`http POST :3000/posts author='serradura' id=3 title='Hulk smash!'``

Add new comment

http --json POST :3000/comments body='Want to smash a giga pudding?' id=3 postId=3

PUT/PATCH

Update comment with id:2

http PATCH :3000/comments/2 Accept:application/json X-Foo:Bar body='updated this using PATCH and httpie!'

DELETE

Remove comment with id:3

http DELETE :3000/comments/3

Resty is just a script so download it and source the file

curl -L http://github.com/micha/resty/raw/master/resty > resty

You can either source passing the url of the API or set it lather

source resty http://localhost:3000

OR

source resty
resty http://localhost:3000

GET

GET /posts

POST

Add new post

POST /posts '{"author": "ed", "id":4, "title":"Want to make a bet?"}' -H "Accept:application/json" -H "Content-Type:application/json"

Add new comment

POST /comments '{"body": "Maybe another day", "id":4, "postId":4}' -H "Content-Type:application/json"

PUT/PATCH

Update comment with id:4

PATCH /comments/4 '{"body":"All right... what do you wanna bet?"}' -H "Content-Type:application/json"

DELETE

Remove comment with id:4

DELETE /comments/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment