Saturday, May 4, 2024
HomeJavaThe best way to ship POST Request with JSON Payload utilizing Curl...

The best way to ship POST Request with JSON Payload utilizing Curl Command in Linux to Check RESTful Net Providers?


There are numerous methods to check your RESTful Net providers developed utilizing Spring
MVC e.g. you should use
RESTAssured
for automation testing or you should use Postman to manually take a look at your RESTful
Net service however nothing beats the elegant, consolation, energy, and ease of
utilizing
curl command
to check RESTful internet service. It’s my favourite instrument to check any RESTful internet
service and I’ve utilized in previous to check varied side of Spring REST
software e.g. testing headers, testing authentication and authorization,
testing totally different content material sorts like
JSON
and
XML, testing cookies, and testing totally different HTTP strategies to carry out CRUD
operations. 
I like
curl command
as a result of I principally work in Linux and it is available there, so I do not
want to put in some other instrument only for testing RESTful APIs.  It additionally
permits me to leverage my Linux experience as I can create scripts to test if
my RESTful internet service is dwell and dealing and carry out different stuff by
writing shell scripts. 
I’ve summarized my testing methods in earlier submit about
testing REST APIs utilizing curl command
however on this explicit submit I’ll discuss one of many frequent however
initially exhausting take a look at to hold out. 

On this article, you’ll study
the way to submit JSON information with curl command to check your Spring REST
software. If you already know curl command, you already know that it is simple to ship POST
request because it lets you specify content material within the command line itself or
load from the file utilizing -d choice. 

Normally, in case your
JSON
is sufficiently small to suit into command line then it’s best to straight write into
shell in any other case you possibly can all the time load that from a file, we’ll look into each
choice on this article.

The best way to ship POST Request utilizing Curl Command  – Mistake

Many builders who’re acquainted with curl command specify the
JSON information
which must be ship on POST command utilizing -d choice as proven beneath:
$ curl -i 
-H "Settle for: software/json" 
-X POST 
-d '{"username":"root","password":"root"}'
https://localhost:8080/springdemo/relaxation/ebook

And that is the place many developer make errors. In first look, the
command look alright however if you happen to run that it’ll fail by saying “Unsupported
Media kind”
e.g.

HTTP/1.1 415 Unsupported Media Kind
Server: Apache-Coyote/1.1
Content material-Kind: textual content/html;charset=utf-8
Content material-Size: 300

They fail to comprehend setting appropriate Content material-Kind, which resulted on this
error. By default, -d sends the Content material-Kind as
“software/x-www-form-urlencoded”, which isn’t accepted on Spring’s aspect and that is why you get this
error. 

To keep away from this, it’s essential ship the proper content material kind through the use of the
“Content material-Kind” header as proven beneath:
-H "Content material-Kind: software/json"

This will probably be accepted on the Spring aspect. Bear in mind, Settle for and Content material-Kind
are two totally different headers. So, though you’re sending the identical
info in earlier instance, it wasn’t inside the proper header. The
Settle for header is used to point the media kind consumer is anticipating
from server.

The best way to use Curl command to Publish JSON information to Spring REST software

Now that you’re conscious of the frequent mistake many Java developer make whereas
utilizing curl command for sending JSON information as payload, it is time to revisit
the proper curl command and perceive it little bit higher:

The proper command is:

$ curl -X POST -H "Content material-Kind: software/json" -d '{"key":"val"}' URL

Right here:

-X specify the HTTP technique which is POST

-H is so as to add a header which is “Content material-Kind”

-d is to specify payload information, since JSON is enclosed in double quotes, you
can use single quotes (‘) in Linux to surround JSON information to straight specify
within the command line. 

URL is the complete URL of your Spring REST software e.g.
https://localhost:8080/springdemo/

Right here is yet another model of identical command for simpler understanding:

$ curl -i 
-H "Content material-Kind: software/json" 
-X POST 
-d '{"username":"root","password":"root"}'
https://localhost:8080/springdemo/relaxation/ebook

The one quotes actually work nicely in Linux to surround JSON information, however if you happen to
are utilizing curl on Home windows then it’s essential escape these double quotes like

$ curl -i 
-H "Content material-Kind: software/json" 
-X POST 
-d "{"username":"root","password":"root"}"
https://localhost:8080/springdemo/relaxation/ebook

This curl command is sweet in case your
JSON payload for POST technique
is sufficiently small but when it’s important to ship a JSON which is simply greater than a
couple of line, it is higher to retailer them in a file and connect that file as
payload utilizing –data choice for
curl command. Let’s examine an instance of that. 

The best way to ship POST Request with JSON Payload from File utilizing Curl command in Linux

You probably have saved your JSON information in a file referred to as payload.json then you possibly can
POST that utilizing following curl command:

$ curl -i 
-H "Content material-Kind: software/json" 
--data @payload.json
https://localhost:8080/springdemo/relaxation/ebook

That is a lot nicer as a result of a lot of the
JSON are actually not that brief and utilizing file additionally lets you keep away from these
escaping of double quotes round JSON information, notably on Home windows. In
Linux, you possibly can nonetheless get round utilizing single quotes but when JSON is giant you
higher use this feature. 

You also needs to keep in mind that it’s essential put the
@earlier than

the filename to make it work, in any other case, curl is not going to embody JSON information
out of your file.

Factors to Notice whereas Sending JSON Payload in POST command utilizing Curl command

Now that you know the way to ship a POST request with JSON information to check a
RESTful Net service utilizing curl command in Linux and Home windows, it is time to
revise a few of the stuff which it’s best to know and keep in mind to keep away from losing
hours in painful
debugging

1) At all times specify the “Content material-Kind” header utilizing -H choice e.g.
-H “Content material-Kind: software/json”.

2) Keep in mind that -d sends content material kind as “software/x-www-form-urlencoded”, which isn’t accepted on Spring aspect whether it is accepting
“software/json”

3) Do not forget to surround JSON information into single quotes in Linux e.g.
-d ‘json-data’.

4) Bear in mind to flee double quotes in home windows whereas
writing JSON
on the command line. 

5) Use –data choice to POST JSON information from a file. 

6) Bear in mind to specify
@ earlier than file title, in any other case
curl is not going to take information from file. 

These are simply a few of the suggestions which it’s best to already know if you’re
acquainted with curl and Spring REST software, however if you wish to study
extra about curl, you too can all the time test these
greatest Linux command line fundamentals programs to study Linux and cRUL higher. 
How to POST JSON data with Curl Command to Test RESTful Web Service in Java and Spring?

That is all about
the way to POST JSON information utilizing curl command to check a RESTful internet service. It isn’t required that RESTful software is written Spring, it is simply
we’ve got used for our instance as a result of Spring MVC gives the best strategy to
develop RESTful internet providers in Java. If you’re new in that area, I
counsel you to undergo REST with Spring course to study growing
RESTful software in Java from scratch.

Different Spring and REST tutorial it’s possible you’ll like:

Thanks for studying this text up to now. In case you like this tutorial about
utilizing cURL command to ship JSON payload to a REST internet providers in Java and
Spring then please share on Fb and Twitter, I might actually respect
that. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments