Please feel free to correct me.
How to access my session id in the cookies sent by server while fetch request.
In broswer api server responds as Http response => Body, Headers, Cookies
Server sends cookie with JSESSIONID. How to access this cookie in fetch .
This my code
fetch("server_url/devices", {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(regData),
})
.then(response => {
return response.json();
})
.then(responseJson => {
console.log(responseJson);
})
.catch(error => {
console.log(error);
});
Add this line to your fetch parameters :
credentials: 'same-origin'
Your fetch method should now get and send the current session's cookie.
I'd invite you though to post questions on Stackoverflow, since this place is about posting issues encountered while using React Native.
Regards
@Bryzz I have added credentials: 'same-origin'. How to aceess cookie values?? can't we access directily or should I use this https://github.com/joeferraro/react-native-cookies
Have you tried add include instead of same-origin?
More info: https://github.com/facebook/react-native/issues/14063
@shergin Since I am new to this, I am not able achive my need.
My server sents session id as a cookie (which I want to console it) , If I can save that seesion id , then I can reuse it for next api requests .can you help in that point of direction.
My code.
```
return fetch(HOSTNAME + '/devices', {
method: 'GET',
credentials: 'same-origin',
headers: {
"authorization": token,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => {
console.log(response)
//console.log(cookies)
return response.json()
})
.then((responseJson) => {
console.log(responseJson)
return responseJson;
})
.catch((error) => {
return reject(error);
});
To access cookies.( I am getting null response)
import CookieManager from 'react-native-cookies';
CookieManager.get(HOST, (err, res) => {
console.log('Got cookies for url', res);
})
```
@iiitmahesh I hardly see why you would need to get the JSESSIONID from your cookie to send other requests to your API. Are you using Spring Security server-side ?
If so, using the line I suggested you on every fetch request should be enough. The cookie would be sent along any fetch request you added this parameter to and accept (or reject) the request depending of the user's authentication, automatically using the stored JSESSIONID.
I shall investigate a bit into this cookie module so I can help you more. Maybe the URL requires to not be restricted in your API ?
@iiitmahesh This problem has been solved?
I have this problem, too
How to solve it
Most helpful comment
Have you tried add
includeinstead ofsame-origin?More info: https://github.com/facebook/react-native/issues/14063