using a Json file in Rest-assured for payload

24,563

Solution 1

After posting the issue with rest-assured team. I have got a fix. I tested the fix and the issue is now resolved.

Message from rest-assured:

It should be fixed now so I've now deployed a new snapshot that should address this issue. Please try version 2.9.1-SNAPSHOT after having added the following Maven repository:

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

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

Solution 2

I use a generic method to read from the json and send that as a string, i.e:

public String generateStringFromResource(String path) throws IOException {

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

}

So in your example:

@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"));
}
Share:
24,563
Bhaskar Mishra
Author by

Bhaskar Mishra

Hi, A Passionate technologist who loves adventure and gadgets. Life is bout family , spirituality and yeah Science. Science has made the difference between Humans and Non Humans. I have used stackoverflow alot to get answers on C#, .net and sql and I would love to be an important contributor here.

Updated on August 14, 2020

Comments

  • Bhaskar Mishra
    Bhaskar Mishra over 3 years

    I have a huge JSON file to be POST as payload of a rest api call 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 error as :

    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 string and that works but I see i can directly pass the file object and this doesnt work.

    After researching enough it seems that it doesnt work. I have opened up issue with rest-assured. https://github.com/jayway/rest-assured/issues/674

  • Bhaskar Mishra
    Bhaskar Mishra about 8 years
    Yes I can send the file content as String but that is one additional step. If i read the method definition it seems it provides a way to pass a file object. I want to use it as it makes the code look cleaner.
  • Luke D. Smith
    Luke D. Smith about 8 years
    It is an additional step in your code, but in terms of performance it's probably actually less expensive than using REST-assured to encode the file. The latest edits you've made should work from what I can see in javadocs, is your json content well formed?
  • Bhaskar Mishra
    Bhaskar Mishra about 8 years
    : Yes the latest code works. I am still wondering why the rest-assured methods are not taking file as input object and throwing up the error. I wrote a common method to make sure for each test I don't have do the string conversion and that works fine.
  • Luke D. Smith
    Luke D. Smith about 8 years
    Glad it's working, you may want to pose that question to the REST-assured guys directly via their pages.