如何将原始数据主体添加到 axios 请求? [英] How can I add raw data body to an axios request?

查看:33
本文介绍了如何将原始数据主体添加到 axios 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Axios 与来自我的 React 应用程序的 API 进行通信.我设法让 GET 请求正常工作,但现在我需要一个 POST 请求.

I am trying to communicate with an API from my React application using Axios. I managed to get the GET request working, but now I need a POST one.

我需要正文为原始文本,因为我将在其中编写 MDX 查询.这是我提出请求的部分:

I need the body to be raw text, as I will write an MDX query in it. Here is the part where I make the request:

axios.post(baseUrl + 'applications/' + appName + '/dataexport/plantypes' + plan,
    {
      headers: { 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxx',
      'Content-Type' : 'text/plain' }
    }).then((response) => {
      this.setState({data:response.data});
      console.log(this.state.data);
    });

这里我添加了内容类型部分.但是如何添加正文部分?

Here I added the content type part. But how can I add the body part?

谢谢.

这是工作 Postman 请求的屏幕截图

Here is a screenshot of the working Postman request

推荐答案

如何使用直接的 axios API?

How about using direct axios API?

axios({
  method: 'post',
  url: baseUrl + 'applications/' + appName + '/dataexport/plantypes' + plan,
  headers: {}, 
  data: {
    foo: 'bar', // This is the body part
  }
});

来源:axios api

这篇关于如何将原始数据主体添加到 axios 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆