Kim Carlo
Kim Carlo

Reputation: 1223

Axios: returns 404 when making POST request

I am new with nodejs and axios. I am working on making the login work as of the moment, but Axios is giving me a 404 not found error whenever I call it inside my controller.

This is how I am doing it. I'm pretty sure I am using axios correctly. I don't have any idea as to why it is giving 404 error.

app.post('/post-login', urlencodeParser, async function (req, res) {
    const instance = axios.create();
    req.body.grant_type = "password";
    req.body.client_id = null;
    req.body.client_secret = null;
    
    const headers = {
      'Content-Type': 'application/x-www-form-urlencoded'
    }

    try{
      const response = await instance.post(jsonData.baseURL+'/auth/login',JSON.stringify(req.body),{headers:headers});
      res.status(200).json(response);
      console.log(response);
    }catch(e){
      res.status(500).json({ message: e });
    }
});

UPDATE:

this is what I get when testing the API endpoint in postman postman data

and this is the headers headers

Upvotes: 0

Views: 4074

Answers (3)

vsriram92
vsriram92

Reputation: 663

For the Typescript people who reached this question from search engines:
Make sure to frame http header values in quotes.
Node's HTTPS request will accept headers without quotes for boolean and numbers, but axios accepts only if the value is surrounded with quotes. So headers will not be passed along with the request in case of axios if we use same headers which we use for https/http in axios.

Non-Working sample:

const config = {
        headers: {'x-mock-match-request-body':false, 'x-mock-match-request-headers':false}
      };
      const response = await axios.post(requestUrl, null, config);

Working sample:

const config = {
        headers: {'x-mock-match-request-body':'false', 'x-mock-match-request-headers':'false'}
      };
      const response = await axios.post(requestUrl, null, config);

Upvotes: 0

Ezequiel Gimenez
Ezequiel Gimenez

Reputation: 53

For people coming from Search Engines like me, if you are using a singleton patttern for accesing an axios instance i.e:

import axios from 'axios';

const dataAPI = axios.create();

export default dataAPI;

make sure your Axios instance is not getting modified by an axios-mock-adapter. If so, call adapter.restore() to get back to initial state.

Note: there's a difference between .restore() and .reset()

Quoting from docs

You can restore the original adapter (which will remove the mocking behavior)

mock.restore();

You can also reset the registered mock handlers with resetHandlers

mock.resetHandlers();

You can reset both registered mock handlers and history items with reset

mock.reset();

Upvotes: 0

jeremynac
jeremynac

Reputation: 1242

I think the problem might be that you're using json encoded data (by using json.stringify) and sending it using the 'application/x-www-form-urlencoded' content type. I would advise you to pass the body without encoding it. Like this:

app.post('/post-login', urlencodeParser, async function (req, res) {
    const instance = axios.create();
    req.body.grant_type = "password";
    req.body.client_id = null;
    req.body.client_secret = null;
    
    const headers = {
      'Content-Type': 'application/x-www-form-urlencoded'
    }

    try{
      const response = await instance.post(jsonData.baseURL+'/auth/login', req.body,{headers:headers});
      res.status(200).json(response);
      console.log(response);
    }catch(e){
      res.status(500).json({ message: e });
    }
});

More on why you should not pass an encoded json to axis with form content-type

Upvotes: 1

Related Questions