when i do an api call with axios like that :
axios.post("/api/post", body).then(r => console.log("r : ", r)).catch(e => console.log("e : ", e))
it works and api return an status of 200 , but when i do the same with fetch like this :
fetch("/api/post", {
method: "POST", body: body, headers: {
'Content-Type': 'application/json'
}
}).catch(e => console.log("e : ", e)).then(r => console.log(r))
the api returns a 400 , i tried it with other tools like postman and ... and they work fine as well but the fetch don't , why do you think this is happening ?
Maybe read the error message of the response?
just says a bad request doesn't give much
Are you the creator of the API?
What can trigger a bad request? Bad request body?
what's body?
i checked this as well , and the thing is it doesn't even get to the handler that i have written , it is just being rejected by the framework i'm using , so i'm guessing it's probabaly bcuz of the request headers that fetch is creating for the request (just a guess) but i'm not sure what would cause these to act so different
@TRGWII, is this a problem that .catch comes before .then?
nah , catch was after then , i just changed it's location for a test , it didn't work in either one
Not in this case
axios might be adding additional headers, like Content-Length, Accept, etc, and I would recomment JSON.stringifying it
Обсуждают сегодня