Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpURLConnection PUT to Amazon AWS S3 403 error

Uploading a file to Amazon S3 using presigned URL which has Signature, Expiration and AccessKey, using the following code I'm able to upload the file using normal java code but same code in Android gives me 403 error. Presigned URL is generate using Amazon SDK

I have read http://developer.android.com/reference/java/net/HttpURLConnection.html and http://android-developers.blogspot.com/2011/09/androids-http-clients.html but not able to figure out what header param should i use, I guess in android it is setting headers in request which server rejects

    HttpURLConnection connection=(HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT"); 
    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
    out.write("This text uploaded as object.");
    out.close();
    int responseCode = connection.getResponseCode();

Exception: 403; Signature don't match :-o

Has anyone come across this issue? Or more details into which Header-parameters are added behind the scenes from android library?

like image 663
Mayank Mehta Avatar asked Feb 17 '23 00:02

Mayank Mehta


1 Answers

please set your content type like this:

connection.setRequestProperty("Content-Type"," ");

because the default for HttpsUrlConnection Automatically generates content type as:

"Content-Type: application/x-www-form-urlencoded"

This will cause signature mismatch.

like image 145
Nison Cheruvathur Avatar answered Mar 06 '23 03:03

Nison Cheruvathur