Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios 400 Bad request in React

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!

like image 374
pconn12 Avatar asked Feb 07 '20 20:02

pconn12


People also ask

How to make a POST request in react using Axios?

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 ...

How to post data from Axios to backend server?

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.

What is the difference between then () and catch () methods in Axios post?

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.

What are the different types of Axios errors?

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.


1 Answers

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

}
like image 172
willieswanjala Avatar answered Oct 01 '22 00:10

willieswanjala