我需要调用此 REST 端点
PATCH https://graph.windows.net/contoso.onmicrosoft.com/users/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394c4a5c4b5758545c795a56574d564a5617565754505a4b564a565f4d175a5654" rel="noreferrer noopener nofollow">[email protected]</a>?api-version=1.5 HTTP/1.1
{
"<extensionPropertyName>": <value>
}
请参阅此处的文档:https://msdn.microsoft.com/en-us/library/azure/dn720459.aspx
我有以下代码来为用户设置一个属性的值:
public async Task<ActionResult> AddExtensionPropertyValueToUser()
{
Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);
var token = await GetAppTokenAsync();
string requestUrl = "https://graph.windows.net/mysaasapp.onmicrosoft.com/users/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b7b1b7a3b0abadf382afbbb1a3a3b1a3b2b2ecadacafaba1b0adb1ada4b6eca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>?api-version=1.5";
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUrl)
{
Content = new StringContent("{ \"extension_33e037a7b1aa42ab96936c22d01ca338_Compania\": \"Empresa1\" }", Encoding.UTF8, "application/json")
};
HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
if (hrm.IsSuccessStatusCode)
{
string jsonresult = await hrm.Content.ReadAsStringAsync();
return View("TestRestCall", new SuccessViewModel
{
Name = "The Title",
Message = "The message",
JSON = jsonresult.ToJson()
});
}
else
{
return View();
}
}
但是,它不是用 204(无内容)来响应,而是用整个用户属性来响应,所以我猜我的其余 CALL 出了问题
最佳答案
我认为你的问题是这一行:
HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
这将向您提供的 URL 发送 HTTP GET 请求,在本例中引用用户“[email protected] ”。这就是为什么您会看到响应中返回的用户的所有属性。
我认为您想要做的是发送您创建的 PATCH HttpRequestMessage。为此,您需要使用 SendAsync 方法并提供 HttpRequestMessage 作为参数。如果您将上面的行更改为以下内容,我想您将设置属性值并获得 204 No Content 响应:
HttpResponseMessage hrm = await hc.SendAsync(request);
关于c# - 如何正确发送PATCH请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30442172/