简体   繁体   English

在axios中发送带有正文的POST请求

[英]Sending POST request with body in axios

I want to send a post request with the body in Axios.我想在 Axios 中发送带有正文的 post 请求。 here is the function I wrote and here the API will work if it only has a header.这是我写的函数,如果它只有一个标题,API 就可以工作。 But in the case for APIs that need a body, it's not working.但是对于需要主体的 API,它不起作用。 (Error since API body is not properly received in the server.). (错误,因为 API 正文未在服务器中正确接收。)。 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.作者已将此答案添加到问题本身中,因此重新格式化问题并将此答案发布在此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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