Search code examples
junitjerseyrest-clientrest-assured

How to use POST method in rest-assured and fetch the value from its response


I want to test POST method using rest-assured. I want to fetch the value returned in the response and use the value as path param in another GET method. Can anyone please provide sample code for POST method and how to fetch the value from response.? Thanks in advance.

Edit :

RequestSpecBuilder builder = new RequestSpecBuilder();
    builder.setBody(input);
    builder.setContentType("application/json; charset=UTF-8");
    RequestSpecification requestSpec = builder.build();
    given()
    .spec(requestSpec)
    .when()
    .post("myURL");

This is the approach i am using for POST and PUT method. My response will produce a JSON output like

{
"Name": "value",
"email": "abc@xxx.com"}

i want to fetch the value of name and use it as a queryparam for another GET method. Can anyone help me to solve this problehow to get the value from JSON response and assign it to a variable ?


Solution

  • This should be covered in the tutorial, but did you try something like this?

    String json = given()
    .spec(requestSpec)
    .when()
    .post("SomeKindOfUrlHere")
    .asString();
    
    System.out.println("Name: " + from(json).get("Name"));
    System.out.println("Email: " + from(json).get("email"));