SharePoint REST API Online 批处理请求问题:获取 400 Bad Request, odata.error Invalid request

Posted

技术标签:

【中文标题】SharePoint REST API Online 批处理请求问题:获取 400 Bad Request, odata.error Invalid request【英文标题】:SharePoint REST API Online batch request issues : getting 400 Bad Request, odata.error Invalid request 【发布时间】:2021-12-21 11:12:50 【问题描述】:

我在使用 _api/$batch 请求隔离批处理请求时遇到了一些麻烦。我确定只是发生了一些简单的错误/问题,但我不能只指出这一点。有什么想法吗?

以下是请求的原始邮递员控制台日志的一些 sn-ps:

POST /_api/$batch HTTP/1.1
Accept: application/json; odata=nometadata
Content-Type: multipart/mixed; boundary="batch_8215457c-c4fb-432a-abf6-7b1f86168c65"
Authorization: Bearer <token deleted>

--batch_8215457c-c4fb-432a-abf6-7b1f86168c65
Content-Type: multipart/mixed; boundary="changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17"
Content-Transfer-Encoding: binary

--changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE https://myOrg.sharepoint.com/sites/mySite/_api/web/lists/getByTitle(myList)/items(27) HTTP/1.1
If-Match: *
Accept: application/json; odata=nometadata

--changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE https://myOrg.sharepoint.com/sites/mySite/_api/web/lists/getByTitle(myList)/items(30) HTTP/1.1
If-Match: *
Accept: application/json; odata=nometadata

--changeset_d9097ba9-2d69-40ca-a301-90cf97aafb17--

--batch_8215457c-c4fb-432a-abf6-7b1f86168c65--

我为每个要删除的项目返回的响应是 400 Bad Request,每个 Batch 响应还包含: "odata.error":"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":"lang":"en-US","value":"无效请求。"

--batchresponse_38add954-a201-4857-a6db-68067eb9bc0a
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 400 Bad Request
CONTENT-TYPE: application/json;odata=nometadata;streaming=true;charset=utf-8

"odata.error":"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":"lang":"en-US","value":"Invalid request."
--batchresponse_38add954-a201-4857-a6db-68067eb9bc0a
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 400 Bad Request
CONTENT-TYPE: application/json;odata=nometadata;streaming=true;charset=utf-8

"odata.error":"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":"lang":"en-US","value":"Invalid request."
--batchresponse_38add954-a201-4857-a6db-68067eb9bc0a--

我一直在挖掘并尝试了一些事情:

尝试从每个变更集中删除“Accept: application/json; odata=nometadata” 在请求内容类型/编码后尝试调整任何换行符 我已尝试将批处理中的一项作为单个删除请求执行(有效) 比较了一些笔记/示例:(Andrew Connell 博客:https://gist.github.com/andrewconnell/dcd89e694475b628d0c7)

有帮助的眼睛能指出错误吗?

【问题讨论】:

【参考方案1】:

这是我对文档的错误解释。

根据Microsoft's documentation,请求 URL 部分声明(强调我的):

请求 URL 由 根服务 URL$batch 选项组成;例如,https://fabrikam.sharepoint.com/_api/$batch 或 https://fabrikam.office365.com/api/v1.0/me/$batch。

但是,“根服务 URL”有点误导,因为它实际上应该是 站点的 URL。我会考虑根服务 URL:

https://myorg.sharepoint.com

所以,在我上面的例子中,它是:

POST <siteURL>/_api/$batch  

或者,更具体的例子:

POST https://yourorg.sharepoint.com/sites/mySite  HTTP/1.1

感谢所有对此提出想法的人。

【讨论】:

以上是关于SharePoint REST API Online 批处理请求问题:获取 400 Bad Request, odata.error Invalid request的主要内容,如果未能解决你的问题,请参考以下文章

从sharepoint rest api下载文件时出现400错误

sharepoint rest api Add ListItem 报错

text 更新项REST API调用SharePoint 2013

javascript 删除项REST API调用SharePoint 2013

Sharepoint 2013:通过REST API确保用户

SharePoint REST API 的 Expand 方法