Now that we know how to send payload to a server using cURL, let's create a new book resource item:
$ curl -H "Content-Type: application/json" -LX POST -d '{"title":"Ultra New Book", "link": "Ultra New Link"}' localhost:8080/api/books/ | jq # POST ie., create a new resource. % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 111 100 59 100 52 99k 89655 --:--:-- --:--:-- --:--:-- 59000 { "id": "6", "title": "Ultra New Book", "link": "Ultra New Link" } % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 46 100 46 0 0 8234 0 --:--:-- --:--:-- --:--:-- 9200 100 301 100 301 0 0 46414 0 --:--:-- --:--:-- --:--:-- 46414 [ { "id": "4", "title": "New Book Title", "link": "New Link" }, { "id": "5", "title": "Book-5", "link": "http://link-to-book5.com" }, { "id": "1", "title": "Book-1", "link": "http://link-to-book1.com" }, { "id": "6", "title": "Ultra New Book", "link": "Ultra New Link" }, { "id": "3", "title": "Book-3", "link": "http://link-to-book3.com" } ]
Here are the commands for quick reference:
- curl -L localhost:8080/api/books | jq # GET CALL
- curl localhost:8080/api/books/3 | jq # GET a single resource.
- curl -LX DELETE localhost:8080/api/books/2 | jq # DELETE a resource.
- curl -H "Content-Type: application/json" -LX PUT -d '{"title": "New Book Title", "link": "New Link"}' localhost:8080/api/books/4 | jq
- curl -H "Content-Type: application/json" -LX POST -d '{"title":"Ultra New Book", "link": "Ultra New Link"}' localhost:8080/api/books/ | jq # POST ie., create a new resource.
And the following is the server's console output:
$ go run main.go 2017/10/09 21:07:50 map[5:{Id:5 Title:Book-5 Link:http://link-to-book5.com} 1:{Id:1 Title:Book-1 Link:http://link-to-book1.com} 2:{Id:2 Title:Book-2 Link:http://link-to-book2.com} 3:{Id:3 Title:Book-3 Link:http://link-to-book3.com} 4:{Id:4 Title:Book-4 Link:http://link-to-book4.com}] 2017/10/09 21:07:50 Starting server at port 8080... 2017/10/09 21:07:56 Received request [GET] for path: [/api/books/] 2017/10/09 21:07:56 ID is 2017/10/09 21:09:18 Received request [GET] for path: [/api/books/3] 2017/10/09 21:09:18 ID is 3 2017/10/09 21:11:38 Received request [DELETE] for path: [/api/books/2] 2017/10/09 21:11:38 ID is 2 2017/10/09 21:11:38 Resource ID 2 deleted: [{Id:2 Title:Book-2 Link:http://link-to-book2.com}] 2017/10/09 21:12:16 Received request [GET] for path: [/api/books/] 2017/10/09 21:12:16 ID is 2017/10/09 21:15:22 Received request [PUT] for path: [/api/books/4] 2017/10/09 21:15:22 ID is 4 2017/10/09 21:16:01 Received request [GET] for path: [/api/books/] 2017/10/09 21:16:01 ID is 2017/10/09 21:17:07 Received request [POST] for path: [/api/books/] 2017/10/09 21:17:07 ID is 2017/10/09 21:17:36 Received request [GET] for path: [/api/books/] 2017/10/09 21:17:36 ID is
An important thing to keep in mind is that even though we use redirection flag -L, for POST requests the body will not be sent. We need to make sure that we are sending it to finally resolved endpoint.
This should give us the basic idea of how to use a REST client.