Anupam Dutta
Anupam Dutta

Reputation: 13

Restassured + Failed to parse the JSON document

While performing POST via rest assured library I am getting the following error:-

Restassured + Failed to parse the JSON document + groovy.json.JsonException: Lexing failed on line: 1, column: 1, while reading 'h', no possible valid JSON value or punctuation could be recognized.

The payload is mentioned in the 'Payload' class. Please help me in solving this JSON parsing issue. I am able to successfully POST, but while retrieving data via Jsonpath class, it is throwing error which is mentioned in the subject line.

package files;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;

import static io.restassured.RestAssured.*;

public class DynamicJson {

    @Test
    public void addBook(){

        String response1 = RestAssured.baseURI="http://216.10.245.166";
        given().log().all().header("Content-Type","application/json")
        .body(Payload.Addbook())
        .when().post("Library/Addbook.php")
        .then()
        .log().all().assertThat().statusCode(200)
        .extract().response().asString();


        JsonPath js1 = new JsonPath(response1);
        String id = js1.get("ID");
        System.out.println(id);





    }

}

package files;

public class Payload {


    public static String Addbook(){

        String payload = "{\r\n" + 
                "   \"name\":\"Learn Appium Automation with Java\",\r\n" + 
                "   \"isbn\":\"bcd\",\r\n" + 
                "   \"aisle\":\"29k27\",\r\n" + 
                "   \"author\":\"John foe\"\r\n" + 
                "}";
        return payload;

    }
}

Upvotes: 0

Views: 5585

Answers (1)

Wilfred Clement
Wilfred Clement

Reputation: 2774

It's a very small mistake

You are parsing the JSON on String response1 which is incorrect

Change it from

String response1 = RestAssured.baseURI="http://216.10.245.166";

To

RestAssured.baseURI = "http://216.10.245.166";
String response1 = given().header().....

The rest of your code is fine

Upvotes: 2

Related Questions