Make Selenium Easy

REST Assured Tutorial 26 – How To Use Java Object As Payload For API Request

Last updated on July 20th, 2020 at 10:51 am

As a part of End to End REST Assured Tutorial , in this post We will learn to pass a JSON payload created using Jackson API or Java Object to request in Rest Assured.

Earlier we have learned to Create JSON payload using Jackson API. Now we will learn to use that JSON payload in a request using Rest Assured.

body(Object object)

When we create a JSON Object using Jackson API then it is a type of ObjectNode class which is a Node that maps to JSON Object structures in JSON content.

Interface RequestSpecification has multiple overloaded body() methods and one of the overloaded methods is RequestSpecification body(Object object). This method will be automatically serialized passed object to JSON or XML and sent with the request.

Rule of Serialization

Whether it will be serialized to JSON or XML that depends upon what we pass as Content-Type. If we pass Content-Type as application/json then Rest Assured will serialize the Object to JSON. To serialize to JSON , Rest Assured will look for which JSON parser is available in class path. First it looks for Jackson. If Jackson is not found it looks for GSON. If both are not found then an exception IllegalArgumentException stating “Cannot serialize because no JSON or XML serializer found in classpath.” will be thrown.

If we pass Content-Type as “application/xml” then Rest Assured will serialize Object in to XML and for that it looks for JAXB library in class path of project. If it is not found then an exception IllegalArgumentException stating “Cannot serialize because no JSON or XML serializer found in classpath.” will be thrown.

If we do not pass any Content-Type then Rest Assured will first try to parse in JSON using Jackson library. If Jackson is not found, it will look for GSON. If both are not found, it will parse in to XML using JAXB. If none found, exception will be thrown.

Note :- This works for the POST and PUT methods only.

So when we create a JSON payload using Jackson API, we can pass it directly to body() method. It will be good to pass Content-Type in Request Specification as well because Rest Assured will parse payload in to JSON or XML but API may not able to understand the payload type.

Rest Assured Example

Output

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 26 – How To Use Java Object As Payload For API Request

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: