Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axios.post sends a GET request #1629

Closed
ligne13 opened this issue Jun 25, 2018 · 9 comments
Closed

axios.post sends a GET request #1629

ligne13 opened this issue Jun 25, 2018 · 9 comments

Comments

@ligne13
Copy link

ligne13 commented Jun 25, 2018

Summary

This code should send a POST request, but sends a GET request ! It happens only when my app is deployed on my production server. But the bug happens before reaching the back-end so I don't understand what is happening and why it is related to the environment. No problem on my local machine.

axios.post('/api/admin/tags/', formData)
         .then((response) => {
             console.log(response.data);
         })
         .catch((error) => {
             console.log(error.response.data);
         });

Context

  • axios version: v0.17.1
@ligne13
Copy link
Author

ligne13 commented Jun 25, 2018

OK just found the solution ! https://stackoverflow.com/questions/47208440/axios-xmlhttprequest-is-sending-get-instead-of-post-in-production-environment

this was caused the trailing slash at the end of the request URI, which was causing a 301 redirect. The POST request was not visible in the XHR tab of Chrome's debug console.

@shirbr510
Copy link

shirbr510 commented Jan 15, 2019

this is quite weird cause I just had the opposite case.

my endpoint expects /api/v1/organizations/ but if I send it /api/v1/organizations and it responds a 301 status, it automatically does a GET request instead of POST.

@fuqi1001
Copy link

Same here, some api routes need tail '/', I just skipped it and it automatically does a GET request

@Goahnary
Copy link

I am having the same issue today. I have a trailing slash axios.post('/shop/cart/', data) and it sends a get request instead of a post.

Package version: 0.18.0

@Goahnary
Copy link

So... is this an issue with the middleware that redirects? Or is this an actual issue with axios?

@KayO-GH
Copy link

KayO-GH commented Nov 26, 2019

With or without a trailing slash, it still sends a GET request. Very frustrating since it causes the request to abort. I am trying to upload a file from a React app to a Django server.

Package version: 0.19.0

@dzineer
Copy link

dzineer commented Dec 2, 2019

Interesting how there is no answer. Did something break in Axios?

@bishoywagih
Copy link

bishoywagih commented Dec 20, 2019

same here i send the request without "/" at the end of the URL and with POST request, but it changed to Get request automatically

"axios": "^0.18",

axios.post('/submit-answer/' + this.questionData.id, {
                answer: this.answer
            })
            .then( response => {
                this.questionData = response.data.detail;
            });

@gajet5
Copy link

gajet5 commented Mar 7, 2020

After analyzing the problem, I identified three items that can create strange behavior.

  1. "/" at the end of the query.
  2. The redirection on the server side.
  3. automatic URL generation

In my case, there was a third situation. I use the i18n plugin.

My request:

axios({
  method: 'POST',
  url: '/account/login',
  data: {
    email: 'login',
    password: 'password',
  } 
});

When prompted, the i18n plugin translated the request with the language in url /en/account/user. After I processed the request on the client side by adding the language to url /en/account/login, the problem went away.

I hope this is helpful.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants