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
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
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
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
Remove comment with id:2 curl -X DELETE http://localhost:3000/comments/2
- -H add header
- -X specify HTTP Method
- -d specify data (e.g. form submit value, JSON, file)
Install with brew install httpie
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.
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
Update comment with id:2
http PATCH :3000/comments/2 Accept:application/json X-Foo:Bar body='updated this using PATCH and httpie!'
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 /posts
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"
Update comment with id:4
PATCH /comments/4 '{"body":"All right... what do you wanna bet?"}' -H "Content-Type:application/json"
Remove comment with id:4
DELETE /comments/4