从呼叫后获取响应标题数据 [英] Get Response Header data from Post Call
本文介绍了从呼叫后获取响应标题数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在调用Microsoft API来创建打印机,但是我需要一种方法来获取有关该帖子的信息的响应标头.
I'm calling a Microsoft API to create a printer however I need a way to get the response header where the information about the post is.
我正在遵循本指南 https://docs.microsoft.com/zh-cn/graph/api/printer-create?view=graph-rest-beta&tabs=csharp ,但不确定如何提取响应从通话中删除,因为我无法为通话分配变量.
I'm following this guide https://docs.microsoft.com/en-us/graph/api/printer-create?view=graph-rest-beta&tabs=csharp but not sure how to extract the response from the call as I can not assign a variable to the call.
await graphClient.Print.Printers
.Create(displayName,manufacturer,model,certificateSigningRequest,physicalDeviceId,hasPhysicalDevice,connectorId)
.Request()
.PostAsync();
操作位置
推荐答案
您可以使用.Net Microsoft Graph客户端库发送HTTP请求并读取响应标头.
You can send HTTP request with the .Net Microsoft Graph client library and read response headers.
类似这样的东西:
var requestUrl = graphClient.Print.Printers.Request().RequestUrl;
var content = "json_content";
var hrm = new HttpRequestMessage(HttpMethod.Post, requestUrl);
hrm.Content = new StringContent(content, System.Text.Encoding.UTF8, "aplication/json");
// Authenticate (add access token)
await client.AuthenticationProvider.AuthenticateRequestAsync(hrm);
// Send the request and get the response.
var response = await client.HttpProvider.SendAsync(hrm);
if (!response.IsSuccessStatusCode)
{
throw new ServiceException(
new Error
{
Code = response.StatusCode.ToString(),
Message = await response.Content.ReadAsStringAsync()
});
}
else
{
// read header values
var headerValues = response.Headers.GetValues("Operation-Location");
}
请求正文的示例:
{
"displayName": "Test Printer",
"manufacturer": "Test Printer Manufacturer",
"model": "Test Printer Model",
"physicalDeviceId": null,
"hasPhysicalDevice": false,
"certificateSigningRequest": {
"content": "{content}",
"transportKey": "{sampleTransportKey}"
},
"connectorId": null
}