Skip to content
Advertisement

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. 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("srctestresourcespicture.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

            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

Wanted result:

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

So, how do I get ——WebKitFormBoundary123 in the request multipart form?

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

        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

Advertisement

Answer

I found out that auto-generated boundary is what I need and it’s not displayed in Rest Assured log, but it’s sent.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement