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

245
Views
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
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!