【问题标题】:How to configure and send multipart request via RestAssured如何通过 RestAssured 配置和发送多部分请求
【发布时间】:2020-03-06 16:17:06
【问题描述】:

我正在尝试配置和发送一个多部分请求,如下所示:

------boundary
Content-Disposition: form-data; name="before"; filename="blob"
Content-Type: application/vnd...+json;type=some_type

{some JSON}
------boundary
Content-Disposition: form-data; name="after"; filename="blob"
Content-Type: application/vnd...+json;type=some_type

{another JSON}
------boundary--

所以我尝试配置一个请求,如下代码所示

RestAssuredConfig config = RestAssured.config().multiPartConfig(
        new MultiPartConfig().defaultCharset(StandardCharsets.UTF_8).
                defaultBoundary("--boundary--"));
MultiPartSpecification m1 new MultiPartSpecBuilder(
        new ObjectMapper().writeValueAsString(some_JSON_transformed_to_HashMap)).
                fileName("blob").controlName("before").
                mimeType(ContentType.TEXT.getAcceptHeader()).
                header("ContentType", "application/vnd...+json;type=some_type").build();
MultiPartSpecification m2 = new MultiPartSpecBuilder(
        new ObjectMapper().writeValueAsString(another_JSON_transformed_to_HashMap)).
                fileName("blob").controlName("after").
                mimeType(ContentType.TEXT.getAcceptHeader()).
                header("ContentType", "application/vnd...+json;type=some_type").build();
RequestSpecification request = RestAssured.given().multiPart(m1).multiPart(m2).
        config(config).
          .header("Content-Type", "multipart/form-data; boundary=" + config.getMultiPartConfig().defaultBoundary());
request.post("some_url");

但是当我尝试执行它时,服务器说资源无效,但我相信 JSON 是正确的,所以我想我的多部分配置不正确。我应该如何配置请求?

【问题讨论】:

    标签: java rest multipartform-data multipart rest-assured


    【解决方案1】:

    您可以尝试类似下面的代码。

    given().auth().preemptive()
                    .basic("Jirausername", "Jirapassword")
                    .header("X-Atlassian-Token", "nocheck")
                    .multiPart(new File("/home/users/cat.log"))
                    .when().post("http://localhost:8181/rest/api/2/issue/STS-223/attachments"); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多