13

I am running into a very strange issue. We are putting an app into production and one of the POST request is turning into a POST followed directly by a GET request to the same URL and the POST is never received in the backend (Laravel). In the chrome network tab it just looks like just a GET but with Burpsuite we can see the POST request.

The code responsible

async store() {
    // This prints post
    console.log(this.method());

    await this.form[this.method()]('/api/admin/users/' + (this.isUpdate() ? this.id : ''));

    if (!this.isUpdate()) {
        this.form.reset();
    }
},

The form.post method content

return new Promise((resolve, reject) => {
    axios[requestType](url, this.data())
    .then(response => {
        this.busy = false;
        this.onSuccess(response.data);
        resolve(response.data);
    })
    .catch(error => {
        this.busy = false;
        if (error.response.status == 400) {
            return this.displayErrors(error.response.data)
        }
        this.onFail(error.response.data.errors);
        reject(error.response.data);
    });
});
Maantje
  • 1,781
  • 2
  • 20
  • 33
  • 2
    You're most likely running into a 301/302 redirect. For example, if your production server attempts to redirect all `http` traffic to `https`, any POST to `http` will get redirected to `https`, but will be turned into a GET request. You can see [this answer](https://stackoverflow.com/questions/35399126/laravel-5-htaccess-https-redirect-on-post-routes-doesnt-work) for a slightly longer explanation. – patricus Nov 09 '17 at 18:47
  • Possible duplicate: [axios.post is sending a GET request](https://stackoverflow.com/questions/46611275/axios-post-is-sending-a-get-request) – Malcolm Sep 14 '21 at 15:19

3 Answers3

43

This question was also answered by me in the Larachat slack forum, and for others sake here is the answer for the next one with such a problem.

Just a little back story. In the chat we found out that it was receiving a 301 error which is a redirect error. I had the same error recently when posting to a url on a staging server, it was working fine locally but not on the staging server.

The problem appeared to be a slash at the end of the post url.

So posting to https://example.com/post/to/ will not work.

Removing the / and posting to https://example.com/post/to will work.

coleander
  • 551
  • 6
  • 10
  • 5
    I dont know why but in my case I had to do the opposite of this!! – markroxor Sep 02 '18 at 12:42
  • Good pointer, thank you! Turned out I was posting to the non-i18n endpoint which caused the backend to redirect to the localized url. And that caused the post-request to end up as get-request. – Samuel Blattner Nov 06 '18 at 15:47
  • I had the exact same issue, the local server worked fine, just not the staging or production server. This was the cause. Thanks! – Paul Jul 09 '19 at 22:18
  • You saved me an interview! I owe you one. – nwillo Jan 23 '20 at 04:03
  • 1
    In my own case, I had to use ```https://www.example.com``` instead of ```https://example.com``` – user3437245 Jun 04 '20 at 08:47
  • indeed this is true if the route is get it does not matter but if it is post try matching Route::post('/some/url'... – Ahmed Aboud Jun 09 '20 at 10:45
  • 1
    thank you sooo much.... In my case my endpoint was http instead of https so it was being redirected and getting the 301 status. Those kind of facepalm moments but felt so good to fix the error – Fernando Martinez Avila Feb 26 '21 at 22:12
0

Just for info, I had the same thing - axios request was being redirected. For me though, it turned out to be some localization middleware causing the redirect!

I set up an alternative route in the Api routes file (again Laravel as in the question), bypassing that middleware (probably where the route should have gone in the first place!). All good now! Schoolboy error I guess!

Nick A
  • 389
  • 1
  • 3
  • 10
0

I confirm the previous answer. And I had the same problem from local to production ambience.

The call to an endpoint like / api / user / store / might be redirect to / api / user / store with a 301 status code and this call was interpreted like a GET that obviously we cant reach (because it not in our route list). So it don't work.

A solution can be to work on Apache configuration (trim last slash) but I prefer to adapt my Axios call.