Javascript Axios 删除带有正文和标题的请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51069552/
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
Axios Delete request with body and headers?
提问by Asfourhundred
I'm using Axios while programing in ReactJS and I pretend to send a DELETE request to my server.
我在 ReactJS 中编程时使用 Axios,我假装向我的服务器发送 DELETE 请求。
To do so I need the headers:
为此,我需要标题:
headers: {
'Authorization': ...
}
and the body is composed of
身体是由
var payload = {
"username": ..
}
I've been searching in the inter webs and only found that the DELETE method requires a "param" and accepts no "data".
我一直在网上搜索,只发现 DELETE 方法需要一个“参数”并且不接受“数据”。
I've been trying to send it like so:
我一直在尝试像这样发送它:
axios.delete(URL, payload, header);
or even
甚至
axios.delete(URL, {params: payload}, header);
But nothing seems to work...
但似乎没有任何效果......
Can someone tell me if its possible (I presume it is) to send a DELETE request with both headers and body and how to do so ?
有人可以告诉我是否有可能(我认为是)发送带有标头和正文的 DELETE 请求,以及如何发送?
Thank you in advance!
先感谢您!
采纳答案by vishu2124
So after a number of tries, I found it working.
所以经过多次尝试,我发现它有效。
Please follow the order sequenceit's very important else it won't work
请按照顺序顺序非常重要,否则将无法正常工作
axios.delete(URL, {
headers: {
Authorization: authorizationToken
},
data: {
source: source
}
});
回答by tarzen chugh
axiox.deletedoes support a request body. It accepts two parameters: url and optional config. You can use config.datato set the request body and headers as follows:
axiox.delete确实支持请求正文。它接受两个参数:url 和可选的配置。您可以使用config.data如下设置请求正文和标头:
axios.delete(url, { data: { foo: "bar" }, headers: { "Authorization": "***" } });
See Here - https://github.com/axios/axios/issues/897
回答by Van_Paitin
Here is a brief summary of the formats required to send various http verbs with axios:
以下是使用 axios 发送各种 http 动词所需的格式的简要摘要:
GET: Two ways- First method
axios.get('/user?ID=12345') .then(function (response) { // Do something })- Second method
axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { // Do something })
GET: 两种方式- 第一种方法
axios.get('/user?ID=12345') .then(function (response) { // Do something })- 第二种方法
axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { // Do something })
The two above are equivalent. Observe the paramskeyword in the second method
以上两个是等价的。观察params第二种方法中的关键字
POSTandPATCH
POST和PATCH
axios.post('any-url', payload).then(
// payload is the body of the request
// Do something
)
axios.patch('any-url', payload).then(
// payload is the body of the request
// Do something
)
DELETE
DELETE
axios.delete('url', { data: payload }).then(
// Observe the data keyword this time. Very important
// payload is the request body
// Do something
)
Key take aways
关键要点
getrequests optionally need aparamskey to properly set query parametersdeleterequests with a body need it to be set under adatakey
get请求可选地需要一个params键来正确设置查询参数delete带有正文的请求需要将其设置在一个data键下
回答by Oluwafemi Sule
axios.deleteis passed a url and an optional configuration.
公理。删除就是传递一个URL和一个可选配置。
axios.delete(url[, config])
axios.delete(url[, config])
The fields available to the configuration can include the headers.
This makes it so that the API call can be written as:
这使得 API 调用可以写为:
const headers = {
'Authorization': 'Bearer paperboy'
}
const data = {
foo: 'bar'
}
axios.delete('https://foo.svc/resource', {headers, data})
回答by ronara
I had the same issue I solved it like that:
我遇到了同样的问题,我是这样解决的:
axios.delete(url, {data:{username:"user", password:"pass"}, headers:{Authorization: "token"}})
回答by Hemantkumar Gaikwad
For Delete, you will need to do as per the following
对于删除,您需要按照以下操作
axios.delete("/<your endpoint>", { data:<"payload object">}})
It worked for me.
它对我有用。
回答by ThunderBird
Actually, axios.deletesupports a request body.
It accepts two parameters: a urland an optional config. That is...
实际上,axios.delete支持请求正文。
它接受两个参数: aurl和一个 optional config。那是...
axios.delete(url: string, config?: AxiosRequestConfig | undefined)
You can do the following to set the response body for the delete request:
您可以执行以下操作来设置删除请求的响应正文:
let config = {
headers: {
Authorization: authToken
},
data: { //! Take note of the `data` keyword. This is the request body.
key: value,
... //! more `key: value` pairs as desired.
}
}
axios.delete(url, config)
Hope this helps someone!
希望这可以帮助某人!
回答by TPPZ
To send an HTTP DELETE with some headers via axiosI've done this:
要通过axios我完成以下操作发送带有一些标头的 HTTP DELETE :
const deleteUrl = "http//foo.bar.baz";
const httpReqHeaders = {
'Authorization': token,
'Content-Type': 'application/json'
};
// check the structure here: https://github.com/axios/axios#request-config
const axiosConfigObject = {headers: httpReqHeaders};
axios.delete(deleteUrl, axiosConfigObject);
The axiossyntax for different HTTP verbs (GET, POST, PUT, DELETE) is tricky because sometimes the 2nd parameter is supposed to be the HTTP body, some other times (when it might not be needed) you just pass the headers as the 2nd parameter.
axios不同 HTTP 动词(GET、POST、PUT、DELETE)的语法很棘手,因为有时第二个参数应该是 HTTP 正文,而其他时候(可能不需要时)您只需将标头作为第二个参数传递.
However let's say you need to send an HTTP POST request without an HTTP body, then you need to pass undefinedas the 2nd parameter.
但是,假设您需要发送一个没有 HTTP 正文的 HTTP POST 请求,那么您需要undefined作为第二个参数传递。
Bare in mind that according to the definition of the configuration object (https://github.com/axios/axios#request-config) you can still pass an HTTP body in the HTTP call via the datafield when calling axios.delete, however for the HTTP DELETE verb it will be ignored.
请记住,根据配置对象的定义(https://github.com/axios/axios#request-config),您仍然可以在调用时通过data字段在HTTP 调用中传递 HTTP 主体axios.delete,但是对于 HTTP DELETE 动词它将被忽略。
This confusion between the 2nd parameter being sometimes the HTTP body and some other time the whole configobject for axiosis due to how the HTTP rules have been implemented. Sometimes an HTTP body is not needed for an HTTP call to be considered valid.
第二个参数有时是 HTTP 主体和有时是整个config对象之间的这种混淆axios是由于 HTTP 规则是如何实现的。有时,HTTP 调用不需要 HTTP 正文即可被视为有效。
回答by jimijuu omastar
I encountered the same problem... I solved it by creating a custom axios instance. and using that to make a authenticated delete request..
我遇到了同样的问题……我通过创建自定义 axios 实例解决了它。并使用它来发出经过身份验证的删除请求..
const token = localStorage.getItem('token');
const request = axios.create({
headers: {
Authorization: token
}
});
await request.delete('<your route>, { data: { <your data> }});

