简体   繁体   English

如何在 RestAssured 中设置边界

[英]How to set boundary in RestAssured

I'm trying to create multipart POST call using RestAssured, but I don't know how to get any boundary there.我正在尝试使用 RestAssured 创建多部分 POST 调用,但我不知道如何在那里获得任何边界。 I tried this code, but it doesn't work.我试过这段代码,但它不起作用。

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

Log日志

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

Wanted result:想要的结果:

------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--

So, how do I get ------WebKitFormBoundary123 in the request multipart form?那么,如何在请求多部分表单中得到------WebKitFormBoundary123呢?

UPDATE: If I use this:更新:如果我使用这个:

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

I will get this, which still doesn't look the same and it doesn't work我会得到这个,它仍然看起来不一样而且它不起作用

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

You can set it as part of Content type您可以将其设置为内容类型的一部分

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

I found out that auto-generated boundary is what I need and it's not displayed in Rest Assured log, but it's sent.我发现自动生成的边界是我需要的,它没有显示在 Rest Assured 日志中,但它已发送。

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

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