Skip to content

Instantly share code, notes, and snippets.

@jqn
Created October 21, 2016 14:21
Show Gist options
  • Select an option

  • Save jqn/e6a2073e26f5db90a55664f53a1fcbad to your computer and use it in GitHub Desktop.

Select an option

Save jqn/e6a2073e26f5db90a55664f53a1fcbad to your computer and use it in GitHub Desktop.
Retrieving csrf token from a django app with react native.
componentDidMount () {
// Get cookies as a request header string
CookieManager.get("http://127.0.0.1:8000/login/", (err, res) => {
// Outputs 'user_session=abcdefg; path=/;'
fetch("http://127.0.0.1:8000/login/", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRFToken': res.csrftoken,
},
body: JSON.stringify({
username: 'user',
password: 'securepassword',
})
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
})
});
}
@mayupat13
Copy link

mayupat13 commented Aug 25, 2017

i am getting this error after running above method on android emulator...
undefined is not an object (evaluating '_reactNative.CookieManager.get')

Any Fix?

@jqn
Copy link
Author

jqn commented Apr 17, 2020

@mayupat13 I know it's been a few years. This is only a code snippet and it requires https://github.com/joeferraro/react-native-cookies to be installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment