如何在 c# Microsoft graph api 请求中获取响应头 [英] How to get response header in c# Microsoft graph api request

查看:36
本文介绍了如何在 c# Microsoft graph api 请求中获取响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 文件复制 使用微软图形 API 在 c# .net 核心中操作.这是一个异步操作,根据文档,它说它返回 响应头检查操作状态,现在的问题是我需要它的响应头,以便我可以检查文件复制操作的状态,但是每次我将空"作为值时,我都尝试过以下代码,

I am trying to do File Copy operation in c# .net core using Microsoft graph API. It is an asynchronous operation, and by doc, it says it returns a location in the response header to check the status of the operation, Now the issue is I need its response header so that I can check the status of file copy operation but every time I am getting 'null' as value, I have tried following code,

DriveItem response = await graphClient.Sites[siteId].Drive.Items[itemId]
                           .Copy(fileName, parentReference)
                           .Request()
                           .PostAsync();

driveItem 返回 null,但我认为至少它应该返回额外的数据承载响应状态和位置.

The driveItem returns null but I think at least it should have returned the additional data-carrying response status and location.

当我在线使用 graph api 时,它可以正常返回响应和位置,但它没有图形客户端服务.

When I use online graph api it just works fine returning response and location, but it doesn't with graph client service.

推荐答案

显然是 msgraph-sdk-dotnet,至少在3.8.0版本可以复现,在d序列化HTTP响应时出现错误em>.可能将其报告为引用存储库中的错误会更有益.

Apparently it is an issue with msgraph-sdk-dotnet, at least it could be reproduced in 3.8.0 version, the error occurs while deserializing HTTP response. Probably it would be more beneficial to report it as a bug in referenced repository.

同时您可以考虑构建一个请求 复制 DriveItem 端点 并处理响应(包括提取 Location 标头),如下所示:

Meanwhile you could consider to construct a request for Copy a DriveItem endpoint and process response (including extracting Location header) as demonstrated below:

var message = graphClient.Sites[siteId].Drive.Items[itemId]
                .Copy(fileName, parentReference)
                .Request()
                .GetHttpRequestMessage();

message.Method = HttpMethod.Post;
var body = new DriveItemCopyRequestBody {Name = fileName, ParentReference = parentReference};
message.Content = new StringContent(graphClient.HttpProvider.Serializer.SerializeObject(body));
message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = graphClient.HttpProvider.SendAsync(message).Result;
           
Console.Write(response.Headers.Location);

这篇关于如何在 c# Microsoft graph api 请求中获取响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆