Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.8k views
by Contributing Tester (78 points)
edited by
{"data":{"email":"[email protected]","password":"password"}}

How to pass this data in request body.

softassert= new SoftAssert();

RestAssured.baseURI = "http://10.0.0.156:5001/master";

RequestSpecification httpRequest = RestAssured.given();

httpRequest.header("Content-Type", "application/json");

//JSONObject requestParams = new JSONObject();

Map<String, Object> data = new HashMap<String, Object>();

//Map<String, Object> details = new HashMap<>();

    data.put("email", "[email protected]");

data.put("password", "password");

httpRequest.body(data.toString());

Response response = httpRequest.post("/loginUser");

1 Answer

1 like 0 dislike
by The go-to Tester (142 points)
selected by
 
Best answer

You above code passes the body as,

{"email":"[email protected]","password":"password"}

But, you are supposed to pass it as,

{"data":{"email":"[email protected]","password":"password"}}

So, you can write something like this.

Map<String, Object> data = new HashMap<String, Object>();

Map<String, Object> details = new HashMap<>();

details.put("email", "[email protected]");
details.put("password", "password");

data.put("data", details);

httpRequest.body(data.toString());

Try that and let me know what you get in comment.

by Contributing Tester (78 points)
Thank You sir for providing your valuable time. I have one more doubt.
-In above code data format is -{password=password, [email protected]}. How to change this map object to Json to get the format as {"email":"[email protected]","password":"password" }
-Response of the content type is "text/html; charset=utf-8" shown.How to change the application.json.
by The go-to Tester (142 points)
@sbala,

you can use

JSONObject requestParams = new JSONObject();

and pass HashMap data directly to the above JSON object.

And pass above JSON object as httpRequest body.
by Contributing Tester (78 points)
Thank you sir.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...