keikaavousi/fake-store-api

User login not working with documenation

grassfinn opened this issue · 0 comments

Your documentation claims it can work with fetch as bellow:

fetch('https://fakestoreapi.com/auth/login',{
            method:'POST',
            body:JSON.stringify({
                username: "mor_2314",
                password: "83r5^_"
            })
        })
            .then(res=>res.json())
            .then(json=>console.log(json))

But, when doing so I receive a
Uncaught (in promise) SyntaxError: Unexpected token 'u', "username a"... is not valid JSON

The only way I found to get it to work with fetch is to change the headers to read application/json

let myHeaders = new Headers();
myHeaders.set('Content-Type', 'application/json');
fetch('https://fakestoreapi.com/auth/login', {
  method: 'POST',
  // headers: myHeaders,
  body: JSON.stringify({
    username: 'mor_2314',
    password: '83r5^_',
  }),
})
  .then((res) => res.json())
  .then((json) => console.log(json));

Not sure if this is an error on the server side or not, but wanted to let you know of the error I was coming up with and the solution I found.