2015-05-25 3 views
6

ho bisogno di chiamare questo riposo endpointCome inviare correttamente una richiesta PATCH

PATCH https://graph.windows.net/contoso.onmicrosoft.com/users/[email protected]?api-version=1.5 HTTP/1.1 

{ 
    "<extensionPropertyName>": <value> 
} 

Si prega di consultare la documentazione qui: https://msdn.microsoft.com/en-us/library/azure/dn720459.aspx

Ho il seguente codice per impostare il valore di una proprietà per un utente:

public async Task<ActionResult> AddExtensionPropertyValueToUser() 
     { 
      Uri serviceRoot = new Uri(azureAdGraphApiEndPoint); 
      var token = await GetAppTokenAsync(); 
      string requestUrl = "https://graph.windows.net/mysaasapp.onmicrosoft.com/users/[email protected]?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(); 
      } 
     } 

Tuttavia, invece di respongint con 204 (Nessun contenuto), la sua risposta con l'intero proprietà utente, quindi credo che qualcosa non va con il mio riposo CALL

http://screencast.com/t/LmoNswKIf2

+0

Hai detto che non stai ricevendo il 204, ma che l'attributo di estensione era stato scritto ? –

risposta

8

Credo che il problema è questa linea:

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

Questo invia una richiesta HTTP GET all'URL che si fornisce, che in questo caso fa riferimento al [email protected] utente" .com". Ecco perché stai vedendo tutte le proprietà dell'utente restituite nella risposta.

Penso che quello che vuoi fare è inviare PATCH HttpRequestMessage che hai creato. Per fare ciò è necessario utilizzare il metodo SendAsync e fornire HttpRequestMessage come parametro. Se modifichi la riga sopra riportata come segue, penso che imposti il ​​valore della proprietà e ottieni la risposta 204 No Content:

HttpResponseMessage hrm = await hc.SendAsync(request); 
+0

plus1 per la presa, :) –

+0

Ciao Jimaco, sto usando sendAsync, ma sto ancora affrontando i problemi: qualche puntatore? http://stackoverflow.com/questions/36023821/how-to-pass-the-following-json-to-a-c-sharp-patch-method-w-or-w-o-javascript-ser/36027802#36027802 –