Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

533
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda