我有一個可以完美髮布數據的POST方法。HttpClient.PutAsync - 「實體只允許使用JSON Content-Type標題進行寫入」
看看文檔看起來PATCH(或PUT)應該看起來完全一樣,只需使用PutAsync
而不是PostAsync
。
那麼做,只是我收到以下錯誤:
+ postResponse {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.NoWriteNoSeekStreamContent, Headers:
{
Cache-Control: private
Date: Mon, 09 Oct 2017 12:19:28 GMT
Transfer-Encoding: chunked
request-id: 60370069-f7c4-421d-842e-b1ee8573c2c2
client-request-id: 60370069-f7c4-421d-842e-b1ee8573c2c2
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Europe","Slice":"SliceB","ScaleUnit":"002","Host":"AGSFE_IN_7","ADSiteName":"DUB"}}
Duration: 3.2626
Content-Type: application/json
}} System.Net.Http.HttpResponseMessage
和響應:
Entity only allows writes with a JSON Content-Type header
足夠的錯誤我也可以看到這個肯定:
ContentType {text/plain; charset=utf-8} System.Net.Http.Headers.MediaTypeHeaderValue
所以這個錯誤是有道理的,但是,我確實告訴它使用JSON,並且它在我的POST方法中使用相同的cod E:
public async Task UpdateToGraph(object UnSerializedContent, string relativeUrl)
{
string accessToken = await _tokenManager.GetAccessTokenAsync();
HttpContent content = new StringContent(JsonConvert.SerializeObject(UnSerializedContent));
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
Client.DefaultRequestHeaders.Add("ContentType", "application/json");
string endpoint = "https://graph.microsoft.com/v1.0" + relativeUrl;
var postResponse = Client.PutAsync(endpoint, content).Result;
string serverResponse = postResponse.Content.ReadAsStringAsync().Result;
}
謝謝'HttpClient的不包含PutAsJsonAsync'的定義,認爲我失去了一些東西? :) –
@Green_qaue你可能缺少一個參考https://msdn.microsoft.com/en-us/library/system.net.http.httpclientextensions.putasjsonasync(v=vs.118).aspx – Nkosi
找到它,謝謝:)另一個問題是,如果我刪除'.Result',我不能再訪問'postResponse.Content' –