axios.post is sending a GET request

Viewed 10913

I have a chrome extension which uses react/axios. In that app I'm sending a post request like so:

export const createComment = payload => {
  const url = `${COMMENTS_BASE_URL}`;
  const promise = axios.post(url, payload);
  return { type: CREATE_COMMENT, promise };
}

Even though it's clearly axios.post(), the browser is sending a GET request to the url, which is not allowed (response 405). I've tried also using axios({ method: 'post', ... }) but the same thing happens with the browser sending a GET request.

4 Answers

Try to remove a trailing slash in COMMENTS_BASE_URL if you have it.

i.e. use '/resource' instead of '/resource/'. We had the same problem.

In my case, My server use https

so, http => https

then problem solved.

This happens then URL is redirected from POST call and GET happens after redirect. Most common redirects are caused by not needed trailing slashes or http redirecting to https

In general this happens because

  1. the original POST request is redirected by the server for some reason and
  2. chrome dev tools hides the initial POST request for some unknown reason in this case.

Typical issues are:

  • Lack of authentication leading to a 302 redirect.
  • Use of http which the server redirects to https with a 301 or 302.

However there could be other reasons so you should inspect the HTTP requests going to your server to understand what's going on. A tool like tcpflow is suitable. On nginx you can tail the access.log file. The idea is to understand what happens to the initial POST request so that the underlying issue can be corrected.

Related