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