如何在RestAssured中设置边界

huangapple go评论104阅读模式
英文:

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

  1. src\test\resources\picture.png
  2. ------------
  3. Content-Disposition: form-data; name = name
  4. Content-Type: text/plain
  5. picture.png
  6. ------------
  7. Content-Disposition: form-data; name = userId
  8. Content-Type: text/plain
  9. 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

  1. src\test\resources\picture.png
  2. ------------
  3. Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = name
  4. Content-Type: text/plain
  5. picture.png
  6. ------------
  7. Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = userId
  8. Content-Type: text/plain
  9. 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.

  1. given().contentType("multipart/form-data")
  2. .config(config.multiPartConfig(multiPartConfig().defaultFileName(null).defaultBoundary("WebKitFormBoundary123")))
  3. .multiPart("file", new File("src\test\resources\picture.png"), "image/png")
  4. .multiPart("name", "picture.png")
  5. .multiPart("userId", 1426373, "text/plain")
  6. .log().all()
  7. .when().post(URL).then().log().all().statusCode(200);

Log

  1. Request method: POST
  2. Request URI: URL
  3. Request params: <none>
  4. Query params: <none>
  5. Form params: <none>
  6. Path params: <none>
  7. Headers: Accept=application/json
  8. Cookies: <none>
  9. Multiparts: ------------
  10. Content-Disposition: form-data; name = file; filename = picture.png
  11. Content-Type: image/png
  12. src\test\resources\picture.png
  13. ------------
  14. Content-Disposition: form-data; name = name
  15. Content-Type: text/plain
  16. picture.png
  17. ------------
  18. Content-Disposition: form-data; name = userId
  19. Content-Type: text/plain
  20. 1426373

Wanted result:

  1. ------WebKitFormBoundary123
  2. Content-Disposition: form-data; name="file"; filename="picture.png"
  3. Content-Type: image/png
  4. src\test\resources\picture.png
  5. ------WebKitFormBoundary123
  6. Content-Disposition: form-data; name="name"
  7. picture.png
  8. ------WebKitFormBoundary123
  9. Content-Disposition: form-data; name="userId"
  10. 1426373
  11. ------WebKitFormBoundary123--

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

UPDATE:
If I use this:

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

I will get this, which still doesn't look the same and it doesn't work

  1. Request method: POST
  2. Request URI: URL
  3. Request params: <none>
  4. Query params: <none>
  5. Form params: <none>
  6. Path params: <none>
  7. Headers: Accept=application/json; boundary=--WebKitFormBoundary123
  8. Cookies: <none>
  9. Multiparts: ------------
  10. Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = file; filename = picture.png
  11. Content-Type: image/png
  12. src\test\resources\picture.png
  13. ------------
  14. Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = name
  15. Content-Type: text/plain
  16. picture.png
  17. ------------
  18. Content-Disposition: form-data; boundary=--WebKitFormBoundary123; name = userId
  19. Content-Type: text/plain
  20. 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

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

huangapple
  • 本文由 发表于 2020年8月13日 21:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63396131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定