英文:
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("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:
Query params:
Form params:
Path params:
Headers: Accept=application/json
Cookies:
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?
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:
Query params:
Form params:
Path params:
Headers: Accept=application/json; boundary=--WebKitFormBoundary123
Cookies:
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"
英文:
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("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?
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
答案1
得分: 1
我发现自动生成的边界是我需要的,它不会显示在Rest Assured日志中,但会被发送。
英文:
I found out that auto-generated boundary is what I need and it's not displayed in Rest Assured log, but it's sent.
答案2
得分: 0
你可以将其设置为内容类型
contentType("multipart/form-data; boundary=--MyBoundary")
英文:
You can set it as part of Content type
contentType("multipart/form-data; boundary=--MyBoundary")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论