Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda