java.lang.IllegalArgumentException: Don't know how to encode XXXX as a byte stream

2,743 views
Skip to first unread message

Marrisa Li

unread,
Jul 3, 2014, 9:09:06 AM7/3/14
to rest-a...@googlegroups.com
Hi,

I'm using RestAssured in my test projects, and using similiar below code to send requests, and all get successful responses, but when I tried to upload a .svg file using the post request, it gets below error, can anybody helps to point out the cause please?

Response response = given().contentType(contentType).headers("X-CSRF-Token", ftok).cookies(userCookies).request().body(body).when().post(url);  


Error message: 
java.lang.IllegalArgumentException: Don't know how to encode ------WebKitFormBoundaryLU6DwOyPfQGHAbDG
Content-Disposition: form-data; name="file"; filename="Circle.svg"
Content-Type: image/svg+xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" version="1.1"><circle cx="0" cy="0" r="10" stroke="black" stroke-width="2" fill="red" /></svg>
------WebKitFormBoundaryLU6DwOyPfQGHAbDG--
 as a byte stream
at com.jayway.restassured.internal.http.EncoderRegistry.encodeStream(EncoderRegistry.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1085)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:952)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at groovy.lang.Closure.call(Closure.java:411)
at com.jayway.restassured.internal.http.HTTPBuilder$RequestConfigDelegate.setBody(HTTPBuilder.java:1086)
at com.jayway.restassured.internal.http.HTTPBuilder$RequestConfigDelegate.setPropertiesFromMap(HTTPBuilder.java:1000)
at com.jayway.restassured.internal.http.HTTPBuilder$post.call(Unknown Source)
at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:801)
at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendRequest(RequestSpecificationImpl.groovy)
at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$sendRequest.call(Unknown Source)
at com.jayway.restassured.internal.filter.RootFilter.filter(RootFilter.groovy:30)
at com.jayway.restassured.filter.Filter$filter.call(Unknown Source)
at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:49)
at com.jayway.restassured.filter.FilterContext$next.call(Unknown Source)
at com.jayway.restassured.internal.RequestSpecificationImpl.invokeFilterChain(RequestSpecificationImpl.groovy:758)
at com.jayway.restassured.internal.RequestSpecificationImpl$invokeFilterChain.callCurrent(Unknown Source)
at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1142)
at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy)
at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$applyPathParamsAndSendRequest.callCurrent(Unknown Source)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:135)
at com.autodesk.tinkercad.pages.APIBase.postAPIResponse_withContentType(APIBase.java:178)
at com.autodesk.tinkercad.testcases.APITestWrapper.uploadResourceFile(APITestWrapper.java:539)
at com.autodesk.tinkercad.testcases.APITest.NewExtrusionAndComparePNG(APITest.java:851)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:46)
at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:37)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


===============================================

Marrisa Li

unread,
Jul 3, 2014, 9:33:47 AM7/3/14
to rest-a...@googlegroups.com
The contentType and payload info I get from developer tool is as below, so I simulate in the same way in my request,  does RestAssured support the file upload now with content-Type in below format?

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryLU6DwOyPfQGHAbDG)

Marrisa Li

unread,
Jul 3, 2014, 9:46:55 AM7/3/14
to rest-a...@googlegroups.com
Seems the error is throw from https://github.com/jayway/rest-assured/blob/master/rest-assured/src/main/java/com/jayway/restassured/internal/http/EncoderRegistry.java
in below method:
 public InputStreamEntity encodeStream( Object contentType, Object data ) throws UnsupportedEncodingException {
.....




   else if ( data instanceof byte[] ) {
            byte[] out = ((byte[])data);
            entity = new InputStreamEntity( new ByteArrayInputStream(
                    out), out.length );
        }

I even tried to convert my payload string to byte[] or ByteArrayInputStream, all get the same error
sO it should be failed in new InputStreamEntity( new ByteArrayInputStream(out), out.length );
Anyone can help on this please?


Johan Haleby

unread,
Jul 3, 2014, 11:00:17 AM7/3/14
to rest-a...@googlegroups.com
You probably want to send the file as multipart.

/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.

Marrisa Li

unread,
Jul 17, 2014, 9:05:12 AM7/17/14
to rest-a...@googlegroups.com
Hi Johan,

Thanks a lot for the good suggestion, I took a look at the blog, and tried with multipart, and the response failed with message "http: no such file"
I check the filePath is correctly point to the expected file. do you know where the problem might be please?

with below code:
Response response = given().multiPart("fileuploadcontrol", new File(filePath)).headers("X-CSRF-Token", ftok).cookies(userCookies).when().post(url);
or 
Response response = given().multiPart("fileuploadcontrol", new File(filePath)).contentType("multipart/form-data").headers("X-CSRF-Token", ftok).cookies(userCookies).when().post(url);
Thanks,
Marrisa

Johan Haleby

unread,
Jul 17, 2014, 12:58:28 PM7/17/14
to rest-a...@googlegroups.com
What's the full stacktrace?

Marrisa Li

unread,
Jul 18, 2014, 7:06:58 AM7/18/14
to rest-a...@googlegroups.com
The test code doesn't get exception, the error is that the response doesn't get expected result, the status code is 400, and the response body has the message "http: no such file", so it should be something wrong in server side?
But manually upload the file is good, so I suppose probably there is something wrong with below code to construct the request, any clues please?


Response response = given().multiPart("fileuploadcontrol", new File(filePath)).headers("X-CSRF-Token", ftok).cookies(userCookies).when().post(url);
or 
Response response = given().multiPart("fileuploadcontrol", new File(filePath)).contentType("multipart/form-data").headers("X-CSRF-Token", ftok).cookies(userCookies).when().post(url);

Message has been deleted

Marrisa Li

unread,
Jul 18, 2014, 7:21:01 AM7/18/14
to rest-a...@googlegroups.com
In developer tools, the request info when I manually upload the request is as below:

Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:2112
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary0YRjyiJqyzLfNBXO
Cookie:pgrp=4; optimizelyEndUserId=oeu1397556718242r0.451497653266415; mp_36746f3bbd808122da0d984449eedd13_mixpanel=%7B%22distinct_id%22%3A%20%223nirknh7rci4%22%2C%22pseudoGroup%22%3A%204%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpap%22%3A%20%5B%5D%2C%22%24people_distinct_id%22%3A%20%221fXqDhuq4Q9%22%2C%22mp_name_tag%22%3A%20%22vanessa%20li%22%7D; euid=1u5ycf08tkq3g; mhcheck=1; conv1fun0=1405681860262|1; optimizelyCustomEvents=%7B%22oeu1397556718242r0.451497653266415%22%3A%5B%22c1_activation_click_signup%22%2C%22%24signup%22%2C%22c1_activation_click_editor%22%2C%22c1_activation_editor%22%2C%22c1_activation_interested%22%5D%7D; VSID=0a86883b9de39a50adcae9eada334f68; SID=3cd1c218000faab6b8e6483f22a32600; LSID=176235269c51759f40fc104e0da075f807c45eb0; optimizelySegments=%7B%22219706793%22%3A%22false%22%2C%22221579783%22%3A%22gc%22%2C%22223586095%22%3A%22direct%22%7D; optimizelyBuckets=%7B%7D; __utma=259940111.55098560.1397556719.1405588461.1405681860.25; __utmb=259940111.4.9.1405681862957; __utmc=259940111; __utmz=259940111.1397556719.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); optimizelyPendingLogEvents=%5B%5D; visid=b853d35fb551645d136d3cdbc95f6da085cd45f5:%7B%22n%22%3A3%2C%22b%22%3A1405681856223955554%2C%22s%22%3A3%7D; 1283tc46795=1405681922810; mp_f3f5db2e5ac2e84088705a7d03dc7037_mixpanel=%7B%22distinct_id%22%3A%20%221u5ycf08tkq3g%22%2C%22pseudoGroup%22%3A%204%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpap%22%3A%20%5B%5D%2C%22%24people_distinct_id%22%3A%20%221LVQQh77XE9%22%2C%22mp_name_tag%22%3A%20%22Tinkercad_Automation%22%7D
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
X-CSRF-Token:98544f9a19f4d43af190c9e9d6cb9815
Request Payload
------WebKitFormBoundary0YRjyiJqyzLfNBXO
Content-Disposition: form-data; name="file"; filename="0fBzMjdbh2U.0.txt"
Content-Type: text/plain


------WebKitFormBoundary0YRjyiJqyzLfNBXO--

Johan Haleby

unread,
Jul 18, 2014, 7:51:19 AM7/18/14
to rest-a...@googlegroups.com
Perhaps the control name should be "file" and not "fileuploadcontrol"? Also content-type may have to be set to text/plain?

/Johan


Marrisa Li

unread,
Jul 21, 2014, 8:29:28 AM7/21/14
to rest-a...@googlegroups.com
Hi Joan,

Thanks you very much, it works!

Though in the documentation, and in other google searched webpages, it suggests to used the previous way as I attached above, it failed. Then I tried with your suggestion, it's uploaded successfully, though the uploaded file content is blank which is not as expected currently, I'll try more on this. The bright side is the blocking issue has went away, Thanks a lot!

Thanks,
Marrisa
Reply all
Reply to author
Forward
0 new messages