26

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.

awwester
  • 9,623
  • 13
  • 45
  • 72
  • So this has something to do with the chrome extension you are embedding, as axios.post() should work on a normal webpage? – xDreamCoding Oct 06 '17 at 20:10

4 Answers4

43

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.

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Pavel Komiagin
  • 708
  • 6
  • 10
  • 2
    you saved my day! – Prashant Feb 09 '18 at 15:20
  • 3
    I actually added a slash at the end and it started working. :-/ This is pretty odd behavior. – Ray Pendergraph Jun 06 '18 at 20:03
  • dude! you save me! I fought with this error for already two hours, I was thinking it was an error on my back end till I got tired of trying to see what and where and I came here. – Nelson May 28 '20 at 03:50
  • In my case I had to use ```https://www.example.com``` instead of ```https://example.com``` – user3437245 Jun 04 '20 at 08:50
  • 2
    Excellent, this solved for me! I found this issue happens when Axios receives a 301 (Moved Permanently), because it makes a redirect but also changes the method to "GET". The root cause is a badly formed URL (as Pavel said, check if slash in the end of the path may or may not be required). Since this may lead to unexpected behavior, I opted to add `maxRedirects: 0` in the Axios config. – Vitor Siqueira Aug 02 '21 at 21:08
  • @Ray Pendergraph I had dropped it as a troubleshooting step, but had to add it back in to get it to work after changing to https – Adam Starrh Dec 03 '22 at 18:54
22

In my case, My server use https

so, http => https

then problem solved.

junho
  • 3,603
  • 3
  • 18
  • 25
  • 1
    This one helped me. – ZephDavies Dec 14 '18 at 12:24
  • This was my case as well – rule_it_subir Nov 04 '21 at 13:53
  • 1
    The general case here is: you sent a request to the wrong URL. That could apply to different domains, different protocols, the wrong path, etc. Sometimes when you use the wrong URL you'll see failure, other times you'll get a redirect. The solution, of course, is not necessarily `http` -> `https` but rather.. double check your URL. See also [Margus Pala's answer](https://stackoverflow.com/a/53579331/666583). – AndrewF Feb 16 '22 at 02:22
4

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

Margus Pala
  • 8,433
  • 8
  • 42
  • 52
4

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.

mateusppereira
  • 910
  • 8
  • 18
lars
  • 640
  • 4
  • 10