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
How to create a session for authentication with fetch API in JS

I have two URLs. The first one is for login post request and the second URL is to call for a json. I can not get the json file from URL, if I am not logged in. How can I create a session so I can do more than one post request or alternatively how can i use JWT or set-cookie for authentication?

const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const login_url = "https://www.example.com/login"
const json_url = "https://www.example.com/users/anastasia"
const data = {"username": "my_username", "password": "my_password"}
const otherParm = {
   headers:{"content-type": 'application/json'},
   body:JSON.stringify(data),
   method:"POST"
};
fetch(login_url, otherParm)
//.then(data=>{return data.json()})
.then(res=>{
    console.log(res.headers.get('jwt'))
   })
.catch(err=>console.log(err))

The response is: distinct_id: 156841653654e68w, jwt: e1616er5hw16e35hw6e5h6eth

fetch(json_url).then(function (response) {
    return response.json();
}).then(function (obt) {
   console.log(obt);
}).catch(function(error) {
   console.error('something went wrong with retrieving the json file');
   console.error(error);
});

The response is: 401 Unauthorized

about 4 years ago · Juan Pablo Isaza
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