Microsoft图形:无效的批处理有效负载格式

时间:2020-06-14 09:14:34

标签: c# azure azure-active-directory microsoft-graph-api azure-ad-b2c

我正在尝试使用update之后的MS Graph Batch API来批量example from the docs个用户列表,但收到错误消息“消息:无效的批量有效载荷格式”。

// batch disable all users
var allUsers = await GraphHelper.client.Users.Request().Select("id").GetAsync();

var batchRequestContent = new BatchRequestContent();
allUsers.Select(user =>
{
  var httpRequest = GraphHelper.client.Users[user.Id].Request().GetHttpRequestMessage();
  httpRequest.Method = HttpMethod.Put;
  httpRequest.Content = GraphHelper.client.HttpProvider.Serializer.SerializeAsJsonContent(new User { AccountEnabled = false });
  return batchRequestContent.AddBatchRequestStep(httpRequest);
});
var userUpdateResult = await GraphHelper.client.Batch.Request().PostAsync(batchRequestContent);

以下禁用用户的代码无需批量处理即可工作。

var user = new User { AccountEnabled = false };
var userResult = await GraphHelper.client.Users[id].Request().UpdateAsync(user);

3 个答案:

答案 0 :(得分:1)

如果我使用lambda运算符,而我将其更改为foreach循环并添加了批处理请求,如下所示,我也会遇到相同类型的错误。

Number

这样,我可以将批量请求添加到batchRequestContent变量,如下图所示。

(JSON Data)Body of Batch Request

注意:JSON批处理请求目前limited到20个单个请求。

答案 1 :(得分:1)

对我来说,解决问题的是将“headers”字段添加到请求正文中,而不仅仅是添加到请求的标题中:

{'requests': [{
                'id': '1',
                'method': 'GET',
                'url': '/...'
            }, {
                'id': '2',
                'method': 'GET',
                'url': '/...'
            }], 'headers': {
                'Content-Type': 'application/json'
            }}

答案 2 :(得分:0)

@ chris-gunawardena和@ shiva-msft-identity我想知道您是否对我有什么疑问,因为在提交POST方法请求以创建时,我收到了“无效的批处理有效载荷格式” SPO列表中的新项目。单独发送的相同请求没有问题,这似乎与我提供身体/内容的方式有关。我的问题在这里(并且我正在使用PowerShell):

MS Graph batching create list item (SPO) Invalid batch payload format

请注意,我在文档页面上的Microsoft JSON批处理视频中看到了它们,在Graph Explorer中,他们使用"ContentType": "application/json",而文档页面上的示例说要提供“ Content-Type”标头(请注意连字号)。在两种情况下,我都尝试将ContentType和Content-Type用作标头(以及与application / json一起使用),但是在两种情况下,其均为“无效的批处理有效载荷格式”。如果我将所有标头放在一起(请求数组中唯一的标头是Content-Type(或ContentType)),则会收到错误Write request id : 2 does not contain Content-Type header or body."。因此,似乎有效载荷格式至少合理地接近“有效”。正是我在Docs(和MS的视频)示例中所看到的。