【问题标题】:How to correctly send a PATCH request如何正确发送 PATCH 请求
【发布时间】:2015-08-07 03:51:25
【问题描述】:

我需要调用这个 REST 端点

PATCH https://graph.windows.net/contoso.onmicrosoft.com/users/username@contoso.onmicrosoft.com?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/usuario1@mysaasapp.onmicrosoft.com?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 有问题

http://screencast.com/t/LmoNswKIf2

【问题讨论】:

  • 你提到你没有得到 204,但是扩展属性是写的吗?

标签: c# rest azure adal


【解决方案1】:

我认为你的问题是这一行:

HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));

这会向您提供的 URL 发送一个 HTTP GET 请求,在本例中该 URL 引用用户“usuario1@mysaasapp.onmicrosoft.com”。这就是为什么您会看到响应中返回的用户的所有属性。

我认为您想要做的是发送您创建的 PATCH HttpRequestMessage。为此,您需要使用 SendAsync 方法并提供 HttpRequestMessage 作为参数。如果您将上面的行更改为以下内容,我认为您将设置属性值并获得 204 No Content 响应:

HttpResponseMessage hrm = await hc.SendAsync(request);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 2022-07-05
  • 2016-10-25
  • 1970-01-01
  • 2019-12-21
  • 2012-07-28
  • 2016-07-03
相关资源
最近更新 更多