如何在RestAssured中设置边界



我正在尝试使用RestAssured创建多部分POST调用,但我不知道如何在那里获得任何边界。我试过这个代码,但它不起作用。

given().contentType("multipart/form-data")
.config(config.multiPartConfig(multiPartConfig().defaultFileName(null).defaultBoundary("WebKitFormBoundary123")))
.multiPart("file", new File("srctestresourcespicture.png"), "image/png")
.multiPart("name", "picture.png")
.multiPart("userId", 1426373, "text/plain")
.log().all()
.when().post(URL).then().log().all().statusCode(200);

日志

Request method: POST
Request URI:    URL
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=application/json
Cookies:        <none>
Multiparts:     ------------
Content-Disposition: form-data; name = file; filename = picture.png
Content-Type: image/png
srctestresourcespicture.png
------------
Content-Disposition: form-data; name = name
Content-Type: text/plain
picture.png             
------------
Content-Disposition: form-data; name = userId
Content-Type: text/plain
1426373

想要的结果:

------WebKitFormBoundary123
Content-Disposition: form-data; name="file"; filename="picture.png"
Content-Type: image/png
srctestresourcespicture.png
------WebKitFormBoundary123
Content-Disposition: form-data; name="name"

picture.png
------WebKitFormBoundary123
Content-Disposition: form-data; name="userId"
1426373
------WebKitFormBoundary123--

那么,如何在请求多部分表单中获得------WebKitFormBoundary123呢?

更新:如果我使用这个:

contentType("multipart/form-data; boundary=--WebKitFormBoundary123")

我会得到这个,它看起来仍然不一样,也不起作用

Request method: POST
Request URI:    URL
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=application/json; boundary=--WebKitFormBoundary123
Cookies:        <none>
Multiparts:     ------------
Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = file; filename = picture.png
Content-Type: image/png
srctestresourcespicture.png
------------
Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = name
Content-Type: text/plain
picture.png             
------------
Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = userId
Content-Type: text/plain
1426373

我发现自动生成的边界是我需要的,它没有显示在Rest Assured日志中,但它已经发送了。

您可以将其设置为内容类型的一部分

contentType("multipart/form-data; boundary=--MyBoundary")

相关内容

  • 没有找到相关文章

最新更新