Simulate file upload

901 views
Skip to first unread message

Tim

unread,
Aug 29, 2011, 2:21:44 AM8/29/11
to REST assured
Hello,

Is there a way to test fileupload?

Regards,
Tim

Jacques du Rand

unread,
Aug 29, 2011, 2:44:02 AM8/29/11
to rest-a...@googlegroups.com
HI Tim
Not yet... Unless you maybe save the file in the db -table ?

But it's a good suggestion - I like it !

What would you say if in the next version(soon to be released) in the url.txt
I add 'hrf'(http-file-parameter)

in urls.txt

===>test-upload
idd||1
hm||post
url||http://127.0.0.1/myupload
hrp||other-request-parm
hrf||/tmp/myfile
<===


onions.txt ( we add fe=file_exists)
===>validate_upload
uidd||1
fe||/tmp/upload_dir/myfile
<===

?
Comments suggestions welcome

Best Regards
Jacques

Johan Haleby

unread,
Aug 29, 2011, 2:47:48 AM8/29/11
to rest-a...@googlegroups.com
Hi, 

Multi-part file uploads doesn't work since it's not supported by http builder. You can how ever read the file as a byte array (if it's not too large) and send it in a post or put request:

byte[] myByteArray = ..
given().body(myByteArray).when().post("/somewhere");

/Johan


On Mon, Aug 29, 2011 at 8:21 AM, Tim <tim.la...@gmail.com> wrote:

Tim

unread,
Aug 29, 2011, 2:54:41 AM8/29/11
to REST assured
Hello,

I tried to do something similar:
Via org.apache.http.entity.mime.MultipartEntity I composed a multipart
message (together with the file I want to send other form data to the
server). But I can get it to work...I think due to
"RestAssured.requestContentType" that can't be set to "multipart/form-
data".

I write the MultipartEntity to a ByteArrayOutputstream (baos) and make
the call with: given().log().request().body(baos.toByteArray()).post("/
rest/template").asString();

After that I get a NullpointerException
java.lang.NullPointerException
at groovyx.net.http.HTTPBuilder
$RequestConfigDelegate.setBody(HTTPBuilder.java:1147)
at groovyx.net.http.HTTPBuilder
$RequestConfigDelegate.setPropertiesFromMap(HTTPBuilder.java:1065)
at groovyx.net.http.HTTPBuilder.post(HTTPBuilder.java:340)
at groovyx.net.http.HTTPBuilder$post.call(Unknown Source)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:
40)
....

Tim

Tim

unread,
Aug 29, 2011, 2:57:41 AM8/29/11
to REST assured
Hello Jacques,

I don't understand 100% what you mean but together with the file I
want to send some other data (form fields) to the server as a
"multipart/form-
data".

Maybe my response to Johan Haleby can explain more whet I am trying to
achieve.


Tim

Johan Haleby

unread,
Aug 29, 2011, 4:19:45 AM8/29/11
to rest-a...@googlegroups.com
Hi,

Interesting. I know people have got it to work with http builder by using a very ugly work-around that I don't like but Rest Assured doesn't expose the necessary methods to make it work right now. If possible I'd like to find another easier solution like this:

File file = ..
given().body(myFile).when().post("..");

This should automatically choose multipart upload. But I don't know how to implement this in a nice way under the hood. If we're lucky it may be as simple as you say that you only change the content-type to multipart but I'm not sure. Perhaps you could checkout the source and try to hard code content type and see if it works with the MultipartEntity?

/Johan

Tim

unread,
Aug 30, 2011, 2:47:10 AM8/30/11
to REST assured
Thx for the quick and helpfull anwers!

I found a sollution @ http://evgenyg.wordpress.com/2010/05/01/uploading-files-multipart-post-apache/
though it is not via RestAssured

Here is the code that does the trick for me:

String url = "http://localhost:8083/myapp/rest/template";

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);

HttpPost post = new HttpPost( url );
MultipartEntity entity = new
MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );

// For File parameters
File f = new File(PATH_TO_FILE);
entity.addPart( "pdf", new FileBody(( File ) f , "application/
pdf" ));

// For usual String parameters
entity.addPart( "name", new StringBody( "testTemplate", "text/
plain", Charset.forName( "UTF-8" )));

post.setEntity( entity );

// Here we go!
String response =
EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );

System.out.println("============== RESPONSE
=======================");
System.out.println(response);
System.out.println("============== END_RESPONSE
=======================");

client.getConnectionManager().shutdown();

Regards,
Tim

On 29 aug, 10:19, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Interesting. I know people have got it to work with http builder by using a
> very ugly work-around<http://groovy.329449.n5.nabble.com/HTTPBuilder-multipart-message-td34...>that
> I don't like but Rest Assured doesn't expose the necessary methods to
> make it work right now. If possible I'd like to find another easier solution
> like this:
>
> File file = ..
> given().body(myFile).when().post("..");
>
> This should automatically choose multipart upload. But I don't know how to
> implement this in a nice way under the hood. If we're lucky it may be as
> simple as you say that you only change the content-type to multipart but I'm
> not sure. Perhaps you could checkout the source and try to hard code content
> type and see if it works with the MultipartEntity?
>
> /Johan
>

Johan Haleby

unread,
Aug 30, 2011, 3:20:02 AM8/30/11
to rest-a...@googlegroups.com
Ok that seems quite complex to me. I would really like to get this working with Rest Assured in a more simple manner. Thanks for your feedback.

/Johan

Frédéric Camblor

unread,
Aug 30, 2011, 4:45:33 AM8/30/11
to rest-a...@googlegroups.com
I definitely would add a "+1" on this feature in rest assured :)

I faced exactly the same problem yesterday (funny ;))

Thanks tim for your code sample ... I'll try it as a temporary workaround...

Maybe could we fill an improvement in the issue tracker for the file upload improvement ?

Frederic
Reply all
Reply to author
Forward
0 new messages