How to put JSON array inside HTTP PUT call using REST Assured?

632 views Asked by At

Am using rest assured to test an API which requires me to conduct an HTTP PUT with a JSON array, for the request body, which only looks like this:

["6", "7", "8", "9", "10"]

Please note that this doesn't require the opening and closing { } - just the square brackets.


pom.xml:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>4.3.2</version>
</dependency>

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-path</artifactId>
    <version>4.3.2</version>
</dependency>

MyApiRestTest.java:

public class MyApiRestTest {
 
      private static final String BASE_URL = "http://localhost:8080/ams/rest";
      private static final String TOKEN = "AMIIinPiyPqMpViABA41HL8xTSsf";

      private static RequestSpecification requestSpec;

      @BeforeAll
      public static void setUpHeaders() {
          RequestSpecBuilder builder = new RequestSpecBuilder();
          builder.addHeader("Token", TOKEN);
          builder.addHeader("Content-Type", "application/json");
          builder.addHeader("Accept", "application/json");
          requestSpec = builder.build();
          requestSpec.config(RestAssured.config().objectMapperConfig(new ObjectMapperConfig(ObjectMapperType.GSON)));
      }


      @Test
      @DisplayName("PUT /v1/uids/{public-id}")
      public void updateUids() {
          String publicId = "ABCD786EFGH45";

          List<String> uids = new ArrayList<>();

          uids.add("6");
          uids.add("7");
          uids.add("8");
          uids.add("9");
          uids.add("10");

          given().spec(requestSpec)
               .pathParam("public-id", publicId)
               .body(uids)
          .when()
               .put(BASE_URL + "/v1/uids/{public-id}")
               .then()
               .statusCode(200);
      }

}

When I run this, I get the following error:

java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <404>.

Tried using different variations / approaches of this JSON array by doing this:

First approach:

String jsonArray = "[\"6\", \"7\", \"8\", \"9\", \"10\"]";

and subsequently:

given().spec(requestSpec)
       .pathParam("public-id", publicId)
       .body(jsonArray)
.when()
       .put(BASE_URL + "/v1/uids/{public-id}")
       .then()
       .statusCode(200);

Second approach:

Using String[] uidsArray = {"6", "7", "8", "9", "10"};

and subsequently:

given().spec(requestSpec)
       .pathParam("public-id", publicId)
       .body(uidsArray)
.when()
       .put(BASE_URL + "/v1/uids/{public-id}")
       .then()
       .statusCode(200);

Both variations give me the same exact error as the one that provided in the full source listing (see above):

java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <404>.

What am I possibly doing wrong?

2

There are 2 answers

1
Hopey One On BEST ANSWER

A 404 error means your resource was not found, which would indicate that the URL is incorrect rather than any issue with the request body content.

1
Andy Nguyen On

It may be about the URL, I can see it is BASE_URL + "/v1/uids/{public-id}". I think you want {public-id} to become an id when it executes, but it does not. Replace that part with the string form of the actual id may solve your issue.