简体   繁体   English

如何使用 REST 将 JSON 数组放入 HTTP PUT 调用中?

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

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: 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: 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: 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:通过执行以下操作尝试使用此 JSON 阵列的不同变体/方法:

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"};使用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?我可能做错了什么?

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. 404 错误表示未找到您的资源,这表明 URL 不正确,而不是请求正文内容有任何问题。

It may be about the URL, I can see it is BASE_URL + "/v1/uids/{public-id}" .可能是关于 URL,我可以看到它是BASE_URL + "/v1/uids/{public-id}" I think you want {public-id} to become an id when it executes, but it does not.我认为您希望{public-id}在执行时成为id ,但事实并非如此。 Replace that part with the string form of the actual id may solve your issue.将该部分替换为实际id的字符串形式可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM