【问题标题】:How to set boundary in RestAssured如何在 RestAssured 中设置边界
【发布时间】:2020-08-13 13:21:23
【问题描述】:

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

given().contentType("multipart/form-data")
            .config(config.multiPartConfig(multiPartConfig().defaultFileName(null).defaultBoundary("WebKitFormBoundary123")))
            .multiPart("file", new File("src\test\resources\picture.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

            src\test\resources\picture.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

src\test\resources\picture.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

        src\test\resources\picture.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

【问题讨论】:

    标签: java rest-assured


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我更新了问题,当我尝试时,您的建议看起来仍然与预期结果不同。我还认为如果不指定任何边界它会自动生成它,对吧?
      猜你喜欢
      • 2021-04-06
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多