Zain Khan
Zain Khan

Reputation: 1814

Error 405 using axios post request in react native

I'm making an post request using axios in my expo react native app but I don't know why my post request is returning error

Request failed with status code 405

When I check my API on postman so it shows me the expected result. I attached the method which hits onPress on button. Kindly check and provide me a solution.

async onSubmit(ref) {
        if (ref.state.emailID && ref.state.password) {
            this.showLoader();
            await axios.post('http://apiurl.com/api/user/Userlogin?emailID=' + ref.state.emailID + '&password=' + ref.state.password,
                { emailID: ref.state.emailID, password: ref.state.password },
                { headers: { 'Content-Type': 'application/json' } })
                .then((response) => {
                    console.log('Innnn');
                    this.hideLoader();
                    console.log("response data: ", response.data);
                }).catch(err => {
                    this.hideLoader();
                    console.log("error: ", err.message);
                })
        }
    }

Upvotes: 1

Views: 1160

Answers (2)

Zain Khan
Zain Khan

Reputation: 1814

I was doing a very silly mistake, the request has been changed from post to get.

Upvotes: 1

Alwaysblue
Alwaysblue

Reputation: 11830

Okay, so there are multiple things which are wrong in you code.

To answer your question at start, I would suggest you to add withCredentials: true}

Try adding {withCredentials: true} in your axios request.

Second, with async and await, you don't need to use .then and catch (rather use try..catch)

async onSubmit(ref) {
    if (ref.state.emailID && ref.state.password) {
    try {
       this.showLoader();
         const networkReq = await axios.post('http://apiurl.com/api/user/Userlogin?emailID=' + ref.state.emailID + '&password=' + ref.state.password,
          {withCredentials: true},
         { emailID: ref.state.emailID, password: ref.state.password },
         { headers: { 'Content-Type': 'application/json' } })
       this.hideLoader();
       console.log(networkReq.data) 
    } catch (error) {
      this.hideLoader();
      console.log("error: ", err.message);
    }
 }
}

Upvotes: 1

Related Questions