Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

530
Views
Uploading Image to AWS presigned post URL using axios

I am trying to upload an image to an S3 bucket using a presigned URL generated using boto3 on Python. I have been using the example python code that was provided in the documentation and was successful (the image got correctly uploaded with the correct Content-Type). However, when trying to do this in Javascript for the purposes of our frontend application, I am really struggling to get it to work.

Here's the example dictionary returned by the backend:

{
    "fields": {
        "AWSAccessKeyId": "AKIAYS3VM3EBIFL7FKE5",
        "key": "posts/623255a762fd9bdfbd13f91a",
        "policy": "<very long string>",
        "signature": "Qvc/sGBHk0uzirzIfR1YmE2kFlo="
    },
    "url": "https://hotspot-storage.s3.amazonaws.com/"
}

Here is the functioning Python code:

response = <json response object>
object_name = 'playground/example_profile_group.png'

response['fields']['Content-Type'] = "image/png"
# Demonstrate how another Python program can use the presigned URL to upload a file
with open(object_name, 'rb') as f:
    files = {'file': (object_name, f)}
    http_response = requests.post(response['url'], data=response['fields'], files=files)

# If successful, returns HTTP status code 204
print(http_response)
print(http_response.text)

Here is the non-functioning Javascript code:

const data = response.data;
let payload = data.fields;
payload['Content-Type'] = 'image/jpeg';
const file = {
    uri: previewPath,
    name: previewPath,
    type: 'image/jpeg',
};
payload.file = file;
const url = data.url;

console.log(payload, "MY PAYLOAD")

axios({
    method: 'post',
    headers: {'Content-Type': 'multipart/form-data'},
    url: url,
    data: payload,
})
    .then(function (response) {
    console.log(response.data, 'uploaded');
    const data = response.data;
    })
    .catch(function (error) {
    console.log(
        'error uploading image',
        error.response.data,
    );
    });
})
.catch(function (error) {
console.log(
    'error getting media link',
    error.response.data,
);
});

This is the error that keeps getting returned:

 error uploading image <?xml version="1.0" encoding="UTF-8"?>
<Error><Code>MalformedPOSTRequest</Code><Message>The body of your POST request is not well-formed multipart/form-data.</Message><RequestId>Q0ES6P4QP75YVVED</RequestId><HostId>eowLxSJQD1xP1EfHPnzGSJzXVGpPjurIMhkdwAD22JMvi9zRoFGg6Bq+mnUt/Lu7DNPY80iBDMc=</HostId></Error>

I have been stuck on this for an absurd amount of time, and cannot tell what I am doing wrong. Any help would be very much appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In order to send a multipart/form-data request body, you'll need to use a FormData instance instead of a JavaScript object.

For example

const { url, fields } = response.data;
const payload = new FormData();
payload.append("file", file); // this is the file blob, eg from <input type="file">
payload.append("Content-Type", "image/jpeg");

// add all the other fields
Object.entries(fields).forEach(([ key, val ]) => {
  payload.append(key, val);
});

// No need to manually set content-type header, your browser knows what to do
const { data: result } = await axios.post(url, payload);
console.log("uploaded", result);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!