0

Here I am again. I'm trying to do a fetch to ask the API for a refreshed token based on previous stored token in localStorage. It gives error 400() and I still don't know why. It is required for the previous token to go on the body of the POST request.

Any help is acceptable, I feel really lost... I've been with this issue for days already...

    fetch(url + '/api-token-refresh/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }, body: JSON.stringify({
            token: localStorage.getItem('token')
        })
    })

I think the problem comes from when I store in localStorage, because on the Request Payload I get

token: "{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0LCJ1c2VybmFtZSI6InJpY2FyZG8uY2Fycm9sYUBnbWFpbC5jb20iLCJleHAiOjE1MzMxMTY2OTQsImVtYWlsIjoicmljYXJkby5jYXJyb2xhQGdtYWlsLmNvbSIsIm9yaWdfaWF0IjoxNTMyNTExODk0fQ.sS3kuC8o51Rix505BFy9nT5w9iygNaKViGq_fVtVChk"}"

Qhat means I don't get just the token...

If you need anything else, just ask and I will add to the question.

14
  • does console.log(localStorage.getItem('token')) return the expected result? Commented Jul 25, 2018 at 9:39
  • pls show what is written with error Commented Jul 25, 2018 at 9:39
  • 2
    @SofiaRibeiro if that's the result of console.log(localStorage.getItem('token')) then you probably just need to replace body: JSON.stringify({ token: localStorage.getItem('token') }) with body: JSON.stringify(localStorage.getItem('token')), since localStorage returns already a key/value pair you don't need to recreate the body Commented Jul 25, 2018 at 9:45
  • 1
    have you tries this request using something like curl or Postman to check that the token is valid on serverside? Commented Jul 25, 2018 at 9:49
  • 1
    See my answer @Signo and thanks for your help! Commented Jul 25, 2018 at 11:35

1 Answer 1

1

I was using JSON.stringify and the token was going on the Request has a string, has soon has I changed the body, the status changed to 200. Thanks for who helped!

fetch(url + '/api-token-refresh/', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }, body: (localStorage.getItem('token'))
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.