Created
October 21, 2016 14:21
-
-
Save jqn/e6a2073e26f5db90a55664f53a1fcbad to your computer and use it in GitHub Desktop.
Retrieving csrf token from a django app with react native.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }) | |
| }); | |
| } |
Author
i am getting this error after running above method on android emulator...
undefined is not an object (evaluating '_reactNative.CookieManager.get')
Any Fix?
Author
@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
This example uses react-native-cookies