【问题标题】:Microsoft graph: Invalid batch payload formatMicrosoft graph:无效的批处理负载格式
【发布时间】:2020-06-14 09:14:34
【问题描述】:

我正在尝试使用example from the docs 之后的 MS Graph Batch API 对用户列表进行批处理 update,但收到错误“消息:无效的批处理负载格式”。

// 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);

【问题讨论】:

  • 您确定 PostAsync(batchRequestContent); 正在发送所需的属性吗?
  • @abestrad 你说得对,当我使用 charls 代理进行检查时,它只是将{"requests": [] } 发送到https://graph.microsoft.com/v1.0/$batch,但与文档中的示例相比,我看不到我缺少什么.

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


【解决方案1】:

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

static async Task<BatchResponseContent> BulkUpdate()
{
    var allUsers = await GraphHelper.graphClient.Users.Request().Top(10).Select("id").GetAsync();

    var batchRequestContent = new BatchRequestContent();
    foreach(var items in allUsers)
    { 
        var httpRequest = GraphHelper.graphClient.Users[items.Id].Request().GetHttpRequestMessage();
        httpRequest.Method = HttpMethod.Patch;
        httpRequest.Content = GraphHelper.graphClient.HttpProvider.Serializer.SerializeAsJsonContent(new User { AccountEnabled = false });
        batchRequestContent.AddBatchRequestStep(httpRequest);
    }
    var userUpdateResult = await GraphHelper.graphClient.Batch.Request().PostAsync(batchRequestContent);
    return userUpdateResult;
}

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

注意:JSON 批量请求目前是 limited 到 20 个单独的请求。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      注意我在文档页面上的 Microsoft 的 JSON Batching 视频中看到,在 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 的视频)中看到的完全一样。

      【讨论】:

      • 仅供参考,我的问题是我需要使用 PowerShell cmdlet ConvertTo-Json 的 -Depth 参数才能正确捕获我的身体内容。
      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2016-11-21
      • 2020-04-11
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多