Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios/XMLHttpRequest is sending GET instead of POST in production environment

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);
    });
});
like image 330
Maantje Avatar asked Nov 09 '17 17:11

Maantje


People also ask

How do I handle Axios POST request?

To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.

Does Axios support post?

A POST request can be made using Axios to “post” data to an endpoint. This endpoint may then use this POST request to perform a certain task or trigger an event. The HTTP post request is performed by calling axios. post() .

How does Axios post work?

Axios POST JSON request A POST request is created with post method. Axios automatically serializes JavaScript objects to JSON when passed to the post function as the second parameter; we do not need to serialize POST bodies to JSON.

Does Axios use request?

Axios works by making HTTP requests with NodeJS and XMLHttpRequests on the browser. If the request was successful, you will receive a response with the data requested. If the request failed, you will get an error. You can also intercept the requests and responses and transform or modify them.


1 Answers

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.

like image 131
coleander Avatar answered Oct 06 '22 10:10

coleander