Javascript 如何将原始数据主体添加到 axios 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51415439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add raw data body to an axios request?
提问by Karim Taha
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.
我正在尝试使用 Axios 与我的 React 应用程序中的 API 进行通信。我设法让 GET 请求工作,但现在我需要一个 POST 请求。
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:
我需要正文是原始文本,因为我将在其中编写 MDX 查询。这是我提出请求的部分:
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?
在这里我添加了内容类型部分。但是我怎样才能添加身体部分呢?
Thank you.
谢谢你。
Edit:
编辑:
采纳答案by Ukasha
回答by Madhu Bhat
You can use the below for passing the raw text.
您可以使用以下内容传递原始文本。
axios.post(
baseUrl + 'applications/' + appName + '/dataexport/plantypes' + plan,
body,
{
headers: {
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxx',
'Content-Type' : 'text/plain'
}
}
).then(response => {
this.setState({data:response.data});
console.log(this.state.data);
});
Just have your raw text within bodyor pass it directly within quotes as 'raw text to be sent'in place of body.
只需将原始文本放在其中body或直接在引号中传递,'raw text to be sent'而不是body.
The signature of the axios post is axios.post(url[, data[, config]]), so the datais where you pass your request body.
axios 帖子的签名是axios.post(url[, data[, config]]),因此这data是您传递请求正文的地方。
回答by hahaha
I got same problem. So I looked into the axios document. I found it. you can do it like this. this is easiest way. and super simple.
我遇到了同样的问题。所以我查看了 axios 文档。我找到了。你可以这样做。这是最简单的方法。而且超级简单。
https://www.npmjs.com/package/axios#using-applicationx-www-form-urlencoded-format
https://www.npmjs.com/package/axios#using-applicationx-www-form-urlencoded-format
var params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);
You can use .then,.catch.
您可以使用 .then,.catch。
回答by Keshav Gera
axios({
method: 'post', //put
url: url,
headers: {'Authorization': 'Bearer'+token},
data: {
firstName: 'Keshav', // This is the body part
lastName: 'Gera'
}
});
回答by Uddesh_jain
There many methods to send raw data with a postrequest. I personally like this one.
有许多方法可以通过post请求发送原始数据。我个人很喜欢这一款。
const url = "your url"
const data = {key: value}
const headers = {
"Content-Type": "application/json"
}
axios.post(url, data, headers)
回答by Query
The only solution I found that would work is the transformRequest property which allows you to override the extra data prep axios does before sending off the request.
我发现唯一可行的解决方案是 transformRequest 属性,它允许您在发送请求之前覆盖 axios 执行的额外数据准备工作。
axios.request({
method: 'post',
url: 'http://foo.bar/',
data: {},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
transformRequest: [(data, header) => {
data = 'grant_type=client_credentials'
return data
}]
})
回答by Adnan shah
You did this in my project
你在我的项目中做了这个
axios({
method: "POST",
url: "https://URL.com/api/services/fetchQuizList",
headers: {
"x-access-key": data,
"x-access-token": token
}
data: {
quiz_name: quizname,
}
})
.then(res => {
console.log("res", res.data.message);
})
.catch(err => {
console.log("error in request", err);
});
This should help
这应该有帮助


