Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios.post is sending a GET request

Tags:

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.

like image 476
awwester Avatar asked Oct 06 '17 17:10

awwester


People also ask

What does Axios post do?

Axios is a promise based HTTP client for the browser and Node. js. Axios makes it easy to send asynchronous HTTP requests to REST endpoints and perform CRUD operations. It can be used in plain JavaScript or with a library such as Vue or React.

How do I send a request body in Axios POST request?

First we're passing the url of the service endpoint. Second we're passing object params which we created above and lastly we will pass headers to the post request. To pass raw data body content-type should be application/json.

How do you send Axios post?

First, it needs the URI of the service endpoint. Second, an object which contains the properties that we want to send to our server should be passed to it. Passing a data object as a parameter to the post method is optional; in this way, a post method is very similar to the get method.

Which is better Axios or fetch?

Without question, some developers prefer Axios over built-in APIs for its ease of use. But many overestimate the need for such a library. The fetch() API is perfectly capable of reproducing the key features of Axios, and it has the added advantage of being readily available in all modern browsers.


2 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.

like image 181
Pavel Komiagin Avatar answered Nov 27 '22 07:11

Pavel Komiagin


In my case, My server use https

so, http => https

then problem solved.

like image 42
junho Avatar answered Nov 27 '22 09:11

junho