I have read every issue on here regarding Axios 400 bad request and I can't find a solution. I have a function I am calling during useEffect that firstly gets data from my API and then later based on other factors may need to POST back to the API.
the GET call works perfect, but the POST call keeps failing.
const home = match.homeTeam.team_name
const homeScore = null
const away = match.awayTeam.team_name
const awayScore = null
const gameID = match.fixture_id
const result = ""
const points = null
const teamName = userInfo.state.teamName
const date = match.event_date
const status = match.statusShort
const realHomeScore = null
const realAwayScore = null
const homeLogo = match.homeTeam.logo
const awayLogo = match.awayTeam.logo
axios.post('/picks/add/', { home, homeScore, away, awayScore, gameID, result, points, teamName, date, status, realHomeScore, realAwayScore, homeLogo, awayLogo })
.then((result) => {
console.log(result.data);
})
.catch((error) => {
console.log(error);
})
I have checked my payload in Network and it's sending exactly what I want.
I get the following error message in my Catch:
Error: Request failed with status code 400
at createError (createError.js:17)
at settle (settle.js:19)
at XMLHttpRequest.handleLoad (xhr.js:60)
The route works fine in Postman, and the POSTS I make in there match up exactly with the payload in my requests on the web. But for some reason they fail.
Does this have to do with making two requests to the same API within the same function? My first request is in an Await so it runs and finishes before the rest of the function goes.
Any input would be greatly appreciated, thanks!
How to Make a Post request in React using Axios Installing Axios. First, we need to install the axios http client library from the npm. Run the below command in your... Post component. This is our Post component which contains input field and textarea field where two way data binding is... Making a ...
Where axios.post () method takes two arguments, the first argument is url and the second argument is the data we need to post to our backend server. At final, we chained with then () method and catch () method. then method is invoked when a post request is successful.
Where axios.post () method takes two arguments, the first argument is url and the second argument is the data we need to post to our backend server. At final, we chained with then () method and catch () method. then method is invoked when a post request is successful. catch method is invoked when a post request is failed and error has occurred.
Edit: axios errors come in three types: message, request and response. To make sure you are handling all possible errors, you could use a conditional block: ughh now I know what it is, one of my inputs isn't correct I guess, which is strange since it worked in Postmam....but atleast I know why now!! I'll work on fixing that now.
You could console.log(error.response)
in your catch block to get a more human-readable object.
Edit: axios
errors come in three types: message
, request
and response
. To make sure you are handling all possible errors, you could use a conditional block:
if (error.response){
//do something
}else if(error.request){
//do something else
}else if(error.message){
//do something other than the other two
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With