简体   繁体   中英

Sending POST request with body in axios

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})
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM