如何在RestAssured中设置边界

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

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

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:

确定