gpt4 book ai didi

java - 如何通过 RestAssured 配置和发送多部分请求

转载 作者:行者123 更新时间:2023-12-01 21:40:02 25 4
gpt4 key购买 nike

我正在尝试配置并发送如下所示的多部分请求:

------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 是正确的,所以我认为我的多部分配置不正确。我应该如何配置请求?

最佳答案

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

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");

关于java - 如何通过 RestAssured 配置和发送多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58793533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com