Understanding cURL step by step with an example for testing REST API

Anurag
6 min readNov 6, 2022

--

This tutorial will help you to understand CURL step by step with examples.

Source: https://www.booleanworld.com/curl-command-tutorial-examples/

1. Curl is a command line tool and library for transferring data with URLs.

2. It supports almost every protocol and it is free and open source.

3. In case you are on windows, you will have to get a curl for Windows.

4. You can either download the curl for windows from here (Download Curl), however, if you can get the git bash, it will make your work easier. You can get Git from here (Download Git)

5. Once you have git installed, you will find git bash on the start menu.

6. Let’s open this, and here you can run your curl command.

7. Type: curl –help

8. It will give you all the options that you can use with curl.

9. Let’s try to run some curl requests.

10. In case you want to get a response, type: curl [URL]

11. So, here in the case of google.com, it says that the document has moved which means there is some redirection. And if you want to follow the redirection, type: curl -L [URL]

12. You see, it has given all the responses and the page of google.com. Similarly, you can curl any website or any HTTP URL entry, it will give you the responses.

13. In case you want to get only the headers, so for that, you can use the curl -I flag [URL]

14. Let’s search for a simple REST API on the browser and let’s go on this website Requires — A hosted REST API ready to respond to your AJAX requests which have some sample rest APIs

15. We can use this with our demo, this website has some fake APIs for testing.

16. For example, this is the GET for a single user.

17. I can use this URL along with the URL provided on the website.

18. Just copy the two URLs mentioned in the below snap in the new tab.

19. It gives us the list of the user

20. If I use this URL https://reqres.in/api/unknown/2 (in my case, the URL can be different in your case) and paste it in git.

So, here it gives me the data and response.

21. If I want the response along with the headers, -I can use curl -I [url]

22. In case just want only headers, you can use the curl — head [URL] or curl -I [URL]

23. In case you want details of client-server interaction, we use -v for verbose.

Curl -v [URL] → This is useful in case you have issues in your request and want to troubleshoot and you want to see exactly what is happening and what is going to the server and what is coming back.

We can see, it has given all the client and server injections.

24. In case you want to get even more details, you can use the –trace flag. For here, you must give a file where you will want to log all the details. So, for example, curl –trace [location] [URL]

25. It will log all the injections in this lograce.txt file

26. See where it’s saved by searching on the start bar.

27. When we use curl –trace-ascii [location2] [URL], you even get the details.

28. In case you want to send headers in a request, then you can use the curl -H “Accept:application/json”

29. Until now we were seeing the gits. Now if you want to post, you have to send the data as well. So let’s go back to sample APIs Reqres — A hosted REST-API ready to respond to your AJAX requests and search for a post request.

30. You can see, we are sending the data as well. Copy the URL of the sample API website along with the endpoint in the new URL and press Enter key.

31. In curl, you can use the -d or –data to send the data.

First copy this part as shown above image and write in the git command as shown in the below picture.

32. When you hit enter, you will see that you have got the response back which says created at this time. This means that our request is successful.

33. Now if you want to use the entire JSON, for that you will have to use the -X flag.

Curl -X POST [header, if any] [URL] -d‘{}’

34. Of course, you can use -I flag.

So, we are getting headers as well.

35. In case you want to use PUT, follow a similar method as above.

curl -I -X PUT -H “Accept:application/json”https://reqres.in/api/user -d‘{}’

36. DELETE: -X DELTE

Curl -I -X DELETE [URL]

Some useful commands for the POST, PUT, and DELETE methods.

If you have any questions or suggestions regarding the blog, please comment down below. In case you have not followed me yet, please do. I hope to see you again for another blog.

Till then, take care!

--

--

No responses yet