簡體   English   中英

Forge Api PATCH請求返回415“不支持的媒體類型”

[英]Forge Api PATCH request returns 415 “unsupported media type”

我正在使用Forge API。 我需要執行PATCH請求。 當我使用Postman發送它時,一切都很好,但是當我使用HttpRequestMessage建立請求時,我得到一個響應-“ 415不支持的媒體類型”。 通過API文檔Content-Type,我將其設置為“ application / vnd.api + json”。

郵遞員的請求正文

在此處輸入圖片說明 郵遞員中的請求標頭

請求對象結構

JObject jsonApi = new JObject();
            jsonApi.Add("version", "1.0");
            JObject attributes = new JObject();
            attributes.Add("displayName", file.FileName);
            JObject data = new JObject();
            data.Add("type", "items");
            data.Add("id", file.ExternalFileId);
            data.Add("attributes", attributes);

            JObject request = new JObject();
            request.Add("jsonapi", jsonApi);
            request.Add("data", data);

            using (var httpClient = new HttpClient())
            {
                HttpRequestMessage http = new HttpRequestMessage
                {
                    RequestUri = new Uri(url),
                    Method = new HttpMethod("PATCH"),
                    Headers =
                    {
                        { HttpRequestHeader.Authorization.ToString(), "Bearer " + userLastAccessToken },
                        { HttpRequestHeader.Accept.ToString(), "application/vnd.api+json" }
                    }
                };  
                http.Content = new StringContent(request.ToString(), Encoding.UTF8, "application/vnd.api+json");
                HttpResponseMessage responseMessage = await httpClient.SendAsync(http);
            }

使用"application/json"再試一次,或使用HttpHeaders.TryAddWithoutValidation"application/vnd.api+json"可能不適用於HttpHeaders的內部驗證):

http.Content = new StringContent(request.ToString(), Encoding.UTF8, "application/json");

要么:

http.Content = new StringContent(request.ToString(), Encoding.UTF8);
http.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.api+json");

好的,我通過這樣設置ContentType解決了問題

http.Content = new StringContent(request.ToString(), Encoding.UTF8);
http.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/vnd.api+json");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM