im pretty new with restAPI, fetch..., so Im having problems understanding this. On a saveButton press, I want to use fetch to post json data. Im on the site: https://example.com/22 The api url is like: https://example.com/api/save/22. 22 is a variable user_id, different for every logged in user.
When Im using routing to navigate, I use:
router.post('',async(req,res)=>{
const signedsessions = req.cookies['connect.sid'];
const projectId = parseInt(req.params['projectID'],10) || 0
...
So I can access the ID and the session quite easily. But how can I access these projectId and signedsessions when using a simple fetch to post a json? Like this:
export function saveData(data, id, session){
id = parseInt(id, 10) || 0;
...
const projId = (id > 0)? '/' + id : '';
const options = {
method:'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': 'connect.sid=' + session
},
body: JSON.stringify(data)
};
return fetch('https://example.com/api/save + projID, options)
.then(res => res.json())
.catch(err => console.log(err));
}
Maybe Im understanding something completely wrong concerning routing and restAPIs. Or is there a trick how to access these informations?