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

247
Vistas
Request in javascript (from python)

I have attempted to create a request in javascript, that has previously worked using python just fine.

the following is an accurate representation of the code I used to post the request with python:


url = 'https://website.com/api/e1'
    
header = {
   'authorization': 'abcd1234'
}
payload = {
    'content': "text",
}
r = requests.post(url, data=payload,headers=header )

This (above) works just fine in python.

now what I did in javascript is the following:

payload = {
    "content": "this is text",
  };
  fetch("https://website.com/api/e1", {
    method: "POST",
    headers: {
      "authorization":
        "abcd1234",
    },
    body: JSON.stringify(payload),
  });

but this is returning the error 400- Bad request

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When using data parameters on python requests.post, the default Content-Type is application/x-www-form-urlencoded(I couldn't find it on the document, but I checked the request. If you know, please leave a comment).

To achieve the same result with fetch, you must do as follows.

const payload = {
  'content': 'this is text',
};
fetch('https://website.com/api/e1', {
  method: 'POST',
  headers: {
    'authorization': 'abcd1234',
  },
  body: new URLSearchParams(payload),
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't need to do this body: JSON.stringify(payload), rather you can simply pass payload in body like this body:payload

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