How to generate WebKitFormBoundary while posting a multipart/form-data request in RestAssured

13,538 views
Skip to first unread message

Rohan Palkar

unread,
Apr 30, 2016, 10:57:02 AM4/30/16
to REST assured
Hi, 

I am trying to perform a file upload as multipart/form-data in RestAssured but probably I am not sending the  WebKitFormBoundary as part of the request.

The call works fine through the below utilities. (Hiding the details due to confidentiality purposes) 

1. POSTMAN - Working

POST <my_url> HTTP/1.1
Host: <my_host_and_port>
<header_#_1> : <header_#_1_value>
<header_#_2> : <header_#_2_value>
Cache-Control: no-cache
Postman-Token: b43e49cf-0560-9909-7d54-ed55a278e0fc
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename=""
Content-Type: 


----WebKitFormBoundary7MA4YWxkTrZu0gW


2. CURL - Working

curl -X POST -H "<header_#_1> : <header_#_1_value>" -H "<header_#_2> : <header_#_2_value>" -H "Cache-Control: no-cache" -H "Postman-Token: 58708f45-232e-e4d1-8dd7-1728e0c32a77" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "file=@<path_to_my_file>" "<my_url>"


3. REST-ASSURED - Not-Working

RestAssured.useRelaxedHTTPSValidation();
HashMap<String, String> my_headers1 = new HashMap<>();
my_headers1.put( xxxxx);
my_headers1.put( xxxxx);
RestAssured.baseURI = "xxxxx";
RestAssured.basePath = "xxxxx";
RestAssured.port = xxxxx;
File myfile = new File(xxxxx);
 
Response upload = given(). 
contentType("multipart/form-data"). 
headers(my_headers1). 
multiPart("file",xccdfFile). 
post();

I am getting a 500 error code & an body stating "unsupported file type" (which is my developers are returning if they find any other file apart from a xml)  while performing this upload. 
As mentioned, the upload via the above 2 utilities works fine.

Could anyone please guide me as to what am I missing in my command. 
Also I would like to understand how to generate the WebKitFormBoundary value in case of RestAssured, if that is the core-issue.

Thanks in advance.

Regards,
Rohan



 
 

Johan Haleby

unread,
May 1, 2016, 1:59:41 PM5/1/16
to rest-a...@googlegroups.com
Hi, 

Maybe you can try the latest snapshot version of REST Assured (2.9.1-SNAPSHOT) that allows you to specify the boundary for multiparts. For example:

given().contentType("multipart/mixed; boundary=abcdef").multiPart(..). ..

or

given().config(config().multiPartConfig(multiPartConfig().defaultBoundary("abcdef"))). ..

So please try version `2.9.1-SNAPSHOT` after having added the following Maven repository:

<repositories>
        <repository>
            <id>sonatype</id>
            <snapshots />
        </repository>
</repositories>

Feedback would be valuable.

/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rohan Palkar

unread,
May 3, 2016, 2:01:29 AM5/3/16
to REST assured
Thanks Johan for your reply.

But we have a restriction of using Rest-Assured 2.3.1 as per our organisational policies. 
Thus, could you suggest a workaround that could work in this context.

I am also not clear on how to generate the boundary value. Is there some utility where I could generate that ?

Regards,
Rohan

Johan Haleby

unread,
May 3, 2016, 2:08:57 AM5/3/16
to rest-a...@googlegroups.com
On Tue, May 3, 2016 at 8:01 AM, Rohan Palkar <palk...@gmail.com> wrote:
Thanks Johan for your reply.

But we have a restriction of using Rest-Assured 2.3.1 as per our organisational policies. 
Thus, could you suggest a workaround that could work in this context.

The only thing I can suggest is to vindicate the restriction. New features are added all the time and if you need this feature you'll need to update.
 

I am also not clear on how to generate the boundary value. Is there some utility where I could generate that ? 

REST Assured does that automatically.

Johan Haleby

unread,
May 3, 2016, 2:10:13 AM5/3/16
to rest-a...@googlegroups.com
And when I said vindicate I ment the opposite (oppose).

唐一鸣

unread,
Aug 15, 2017, 1:44:11 AM8/15/17
to REST assured
hello,did you solve the problem using rest-assured?
and how you solve
how you paa the boundary values,thanks!

i need to pass this:

------WebKitFormBoundaryTWfMVJErBoLURJIe Content-Disposition: form-data; name="imgFile"; filename="designer.jpg" Content-Type: image/jpeg ------WebKitFormBoundaryTWfMVJErBoLURJIe Content-Disposition: form-data; name="x" 0 ------WebKitFormBoundaryTWfMVJErBoLURJIe Content-Disposition: form-data; name="y" 1.4333333333333333 ------WebKitFormBoundaryTWfMVJErBoLURJIe Content-Disposition: form-data; name="w" 514.5666666666667 ------WebKitFormBoundaryTWfMVJErBoLURJIe Content-Disposition: form-data; name="h" 514.5666666666667 ------WebKitFormBoundaryTWfMVJErBoLURJIe--



在 2016年4月30日星期六 UTC+8下午10:57:02,Rohan Palkar写道:
Reply all
Reply to author
Forward
0 new messages