Even though, Postman is a great tool and chrome plugin to test RESTful Web services for Java developers, I often find myself using "curl" or "cURL" command in Linux for testing RESTful Web Services, particularly Java web services built using Spring or Spring Boot framework. The curl command is easily available on most of Linux hosts and you just need to type a command to sent various types of HTTP requests to your application from Linux command line, as shown here. Though, many RESTful web developer and tester knows this trick, they mainly use GET request to access data from web service and many of them struggle to send HTTP POST request containing JSON data to create new resources.
This is obvious because most of the time we consume JSON response instead of sending JSON request using HTTP POST method, but knowing how to do that pays off.
Once you know the command and trick to send POST request, containing JSON formatted data in the body, you can do all kind of testing from Linux command line itself.
In this article, I'll show you how to test Spring RESTful Web Services using curl command, especially by sending JSON request using HTTP POST method.
How to send JSON data to REST API using curl command
Here is the curl command you can use to send an HTTP POST request with JSON data in the body
$ curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' URL
Here -X specify the HTTP method you are using, -H specifies the content type, which is application/json and -d is for data which is embedded into HTTP request body, and finally the URL specifies the RESTful web service end points.
For example, you can use curl command to send JSON data to the JSONPlaceholder API using the POST method, which is a free, open-source fake REST API for testing and prototyping:
$ curl -X POST \
-H "Content-Type: application/json" \
-d '{"title": "foo", "body": "bar", "userId": 1}' \
https://jsonplaceholder.typicode.com/posts
In this example also:
-X POST specifies the HTTP method as POST.
-H "Content-Type: application/json" sets the request header to indicate that the content is JSON.
-d '{"title": "foo", "body": "bar", "userId": 1}' specifies the JSON data to be sent in the request body.
and https://jsonplaceholder.typicode.com/posts is the URL of the endpoint to which the request is sent.
While I still prefer to Postman for testing because it provides a GUI where you can save URLs in collections, use environment variables, and also play with headers and data, sometime you just need a curl request to solve your problem.
That's all about how to send HTTP POST request with JSON data in Linux using curl command. You have seen the example of how we sent a POST request to create a new post on the JSONPlaceholder API with the provided JSON data.
Other Linux tutorials and examples you may like
- 10 examples of find command in UNIX (examples)
- 10 examples of grep command in UNIX (examples)
- 10 examples of date command in Linux (examples)
- How to get an IP address from the hostname and vice-versa in Linux (command)
- 10 examples of xargs command in Linux (examples)
- 10 Courses to learn Linux commands for beginners (best courses)
- 10 examples of tar command in UNIX (examples)
- 10 examples of Vim in UNIX (examples)
- How to create, update and delete soft link in UNIX (command)
- How to delete empty files and directory in UNIX (solution)
- How to make a directory tree in one command? (example)
- How to how long argument of a process in Solaris (command)
- UNIX command to find out how long a process is running? (answer)
- UNIX command to find the size of the file and directory? (command)
- 5 examples of sort command in Linux (examples)
- 5 examples of kill command in Linux (examples)
- 10 examples of chmod command in UNIX (examples)
- 10 tips for working fast in UNIX? (tips)
If you know any other way to test your REST API using cURL command feel free to share in comments.
Happy Testing and Development !!
1 comment :
This only work if you request is small or you have already copied the curl command and then you just edit like change a couple of parameters otherwise editing JSON with escape characters in Linux is lot of pain.
Post a Comment