Multipart/mixed 使用请求 npm 包请求 google 驱动器批量删除
Multipart/mixed request for google drive bulk deletion using request npm package
我正在尝试使用 rest API 从 google 驱动器中批量删除文件。所以我正在构建批量删除请求的请求,我能够使用类似的请求框架方法 实现删除,但我试图在不发送正文而不是在请求对象中发送多部分数组的情况下实现这一点。我收到以下响应正文
的错误 400
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
这是我的请求对象,失败
const _multipart = []
arrayOfFileIds.forEach((current) => {
const obj = {
body: 'Content-Type: application/http\n\n' +
'DELETE https://www.googleapis.com/drive/v3/files/' +
current + '\nAuthorization: Bearer ' + authToken
}
_multipart.push(obj)
})
const requestOptions = {
url: 'https://www.googleapis.com/batch/drive/v3',
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed'
},
multipart: _multipart
}
下面的请求对象正在工作
const boundary = 'END_OF_PART'
const separation = '\n--' + boundary + '\n'
const ending = '\n--' + boundary + '--'
const requestBody = arrayOfFileIds.reduce((accum, current) => {
accum += separation +
'Content-Type: application/http\n\n' +
'DELETE https://www.googleapis.com/drive/v3/files/' +
current +
'\nAuthorization: Bearer ' + authToken
return accum
}, '') + ending
const requestOptions = {
url: 'https://www.googleapis.com/batch/drive/v3',
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed; boundary=' + boundary
},
body: requestBody
multipart: _multipart
}
修改点:
- 访问令牌可以包含在请求中header。
- 从
body
中取出每个批处理请求的 Content-Type
。
当这些点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
const _multipart = [];
arrayOfFileIds.forEach((current) => {
const obj = {
"Content-Type": "application/http",
body: "DELETE https://www.googleapis.com/drive/v3/files/" + current + "\n",
};
_multipart.push(obj);
});
const requestOptions = {
url: "https://www.googleapis.com/batch/drive/v3",
method: "POST",
headers: {
"Content-Type": "multipart/mixed",
Authorization: "Bearer " + authToken,
},
multipart: _multipart,
};
注:
- 我测试上面修改后的脚本,没有出现错误。可以删除文件。当您测试上述脚本时,当出现错误时,请再次确认脚本和访问令牌。
参考:
我正在尝试使用 rest API 从 google 驱动器中批量删除文件。所以我正在构建批量删除请求的请求,我能够使用类似的请求框架方法
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
这是我的请求对象,失败
const _multipart = []
arrayOfFileIds.forEach((current) => {
const obj = {
body: 'Content-Type: application/http\n\n' +
'DELETE https://www.googleapis.com/drive/v3/files/' +
current + '\nAuthorization: Bearer ' + authToken
}
_multipart.push(obj)
})
const requestOptions = {
url: 'https://www.googleapis.com/batch/drive/v3',
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed'
},
multipart: _multipart
}
下面的请求对象正在工作
const boundary = 'END_OF_PART'
const separation = '\n--' + boundary + '\n'
const ending = '\n--' + boundary + '--'
const requestBody = arrayOfFileIds.reduce((accum, current) => {
accum += separation +
'Content-Type: application/http\n\n' +
'DELETE https://www.googleapis.com/drive/v3/files/' +
current +
'\nAuthorization: Bearer ' + authToken
return accum
}, '') + ending
const requestOptions = {
url: 'https://www.googleapis.com/batch/drive/v3',
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed; boundary=' + boundary
},
body: requestBody
multipart: _multipart
}
修改点:
- 访问令牌可以包含在请求中header。
- 从
body
中取出每个批处理请求的Content-Type
。
当这些点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
const _multipart = [];
arrayOfFileIds.forEach((current) => {
const obj = {
"Content-Type": "application/http",
body: "DELETE https://www.googleapis.com/drive/v3/files/" + current + "\n",
};
_multipart.push(obj);
});
const requestOptions = {
url: "https://www.googleapis.com/batch/drive/v3",
method: "POST",
headers: {
"Content-Type": "multipart/mixed",
Authorization: "Bearer " + authToken,
},
multipart: _multipart,
};
注:
- 我测试上面修改后的脚本,没有出现错误。可以删除文件。当您测试上述脚本时,当出现错误时,请再次确认脚本和访问令牌。