Sending POST request with body in axios

Viewed 2894

I want to send a post request with the body in Axios. here is the function I wrote and here the API will work if it only has a header. But in the case for APIs that need a body, it's not working. (Error since API body is not properly received in the server.). Is this the proper way to do this.

const post = (endPoint = '', header = {}, body = {}) => {
       return axios({method: 'POST', url: `${urls.apiBaseUrl}/${endPoint}/?tenant_id=3`, headers: header, body: body})
}
1 Answers

Here is the proper way to do this. What you have to do is just pass the body as data. (Not body)

const post = (endPoint = '', header = {}, body = {}) => {
        return axios({method: 'POST', url: `${urls.apiBaseUrl}/${endPoint}/?tenant_id=3`, headers: header, data: body})
}

Note: Answered by the original author. The author has been added this answer into the question itself, so reformatted the question and posted this answer here.

Related