Multipart PUT request in rest assured to upload file to S3

1,230 views
Skip to first unread message

Harish Talanki

unread,
Jul 18, 2017, 2:25:05 AM7/18/17
to REST assured
Hi All,

I am trying to do a PUT request to S3 in order to upload a file. Below the Rest Assured code that I have so far,

String putURL = "A lengthy URL that is generated dynmaically";
String fileId = "A random 40 digit key generated by our server";
String cKey = "some key given by amazon";
String cMD5 = "some md5 hash value";

Response r = given().contentType("multipart/mixed").
                headers("x-amz-header1",cKey,
                        "x-amz-header2",cMD5,
                        "x-amz-header7",accountId,
                        "x-amz-header6",fileId,
                        "x-amz-header5","abc",
                        "x-amz-header4","image/jpeg",
                        "x-amz-header3","true",
                        "Content-Type","application/octet-stream").
                multiPart(new File("src/test/resources/media/ToBeRemoved.jpg")).
                put(putURL); 
System.out.println("*********Response code: "+r.getStatusCode());


I get 400(Bad Request) as a response from server. I tried the same request using the rest client like POSTMAN and also using the curl command on my machine. It just works fine.

So there is something wrong in the way I am using multipart upload for a PUT request. Any help is much appreciated. 

Thanks,
Harisha Talanki

harisha...@gmail.com

unread,
Jul 24, 2017, 3:26:17 PM7/24/17
to REST assured
I have kind of partially figured out the problem.

The lengthy URL that is generated dynamically is already encoded. Since REST Assured will again encode the URL, I was getting 400.

TRY1:

Solution for that is, 
given().urlEncodingEnabled(false)

and I am also separating out the query parameters as follows,

queryParam("AWSAccessKeyId", awsAccessKeyId).
queryParam
("x-amz-security-token", sToken).
queryParam
("Signature", signature).

Now, I don't get 400, but I am getting 403(Signature provided does not match).  When I see the response body, AWS is sending the encoded signature.

For eg. If I have specified ...?Signature=AQcItZeRBWsRWVwjsNNt2ftJUL8%3D in the URL, then in the response I get,

<SignatureProvided>AQcItZeRBWsRWVwjsNNt2ftJUL8=</SignatureProvided>

TRY2:

I realized that Rest Assured is decoding the query parameters that I am passing. So I encoded the actual signature query param so that it will be same when Rest Assured decodes it.

queryParam("AWSAccessKeyId", awsAccessKeyId).
queryParam
("x-amz-security-token", sToken).
queryParam
("Signature", URLEncoder.encode(signature)).

Still I get the same error. Surprisingly, in the error message it shows the same signature that I require, but still same error,

<SignatureProvided>AQcItZeRBWsRWVwjsNNt2ftJUL8%3D</SignatureProvided>

I am still stuck with this. Anyone else have faced the same issue? 

Thanks,
Harisha Talanki

Johan Haleby

unread,
Jul 24, 2017, 3:28:03 PM7/24/17
to rest-a...@googlegroups.com
Not sure if it helps but if I remember it correctly you must specify parameters in the right order when invoking AWS resources.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

harisha...@gmail.com

unread,
Jul 24, 2017, 4:48:45 PM7/24/17
to REST assured
Hi Johan,

I tried with the exact order that is specified in the AWS documentation and I still get the same error.

I have changed the order in the POSTMAN and I get proper response. So as long as the order of parameters is concerned I don't think its an issue.

Thanks,
Harisha Talanki
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

harisha...@gmail.com

unread,
Jul 27, 2017, 11:40:14 AM7/27/17
to REST assured
Hi Johan,

I finally found out the solution with the help of your answer in Git issues.


This is what solved my problem,

You can instruct RA not to use supply the "charset=ISO-8859-1"
automatically by using the EncoderConfig
https://github.com/jayway/rest-assured/wiki/Usage#encoder-config, for
example,
 given().
config
(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false))).

Thanks a ton. 

Regards,
Harisha Talanki

Johan Haleby

unread,
Jul 27, 2017, 2:51:57 PM7/27/17
to rest-a...@googlegroups.com
Thanks for sharing, glad you got it working.

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

Praveen Subramani

unread,
Jun 14, 2019, 12:24:35 AM6/14/19
to REST assured
Hi Harish,

 I am also working on the similar  test case for file transfer from local to S3. I believe you have already done the below steps.
Could you please share if you have any public repo for referring. Or sample code

Harisha M

unread,
Apr 27, 2020, 8:20:33 AM4/27/20
to REST assured
Hi Harish,

I am working on AWS api automation with Restassured, could you please share some sample code to configure AWS signature authentication.

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