Using Json file in Rest-sure for payload

I have a huge JSON file that will be POST as the payload of the api call to rest for testing purposes. I tried something like:

    public void RestTest() throws Exception {
    File file = new File("/Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json");
    String content = null;

    given().body(file).with().contentType("application/json").then().expect().
            statusCode(200).
            body(equalTo("true")).when().post("http://devsearch");


}

and get an error like:

java.lang.UnsupportedOperationException: Internal error: Can't encode /Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json to JSON.

I can run by reading the file and passing the body as a string, and this works, but I see that I can directly pass the file object, and this does not work.

After a thorough study, it seems that it does not work. I discovered a problem with self-confidence. https://github.com/jayway/rest-assured/issues/674

+4
source share
2 answers

, . . , .

:

, , . 2.9.1-SNAPSHOT Maven:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>

: https://github.com/jayway/rest-assured/issues/674#issuecomment-210455811

+2

json , :

public String generateStringFromResource(String path) throws IOException {

    return new String(Files.readAllBytes(Paths.get(path)));

}

, :

@Test
public void post() throws IOException {

   String jsonBody = generateStringFromResource("/Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json")

    given().
            contentType("application/json").
            body(jsonBody).
    when().
            post("http://dev/search").
    then().
            statusCode(200).
            body(containsString("true"));
}
+2

All Articles