Forge Api PATCH请求返回415“不支持的媒体类型”

时间:2019-02-12 20:45:27

标签: c# asp.net autodesk-forge

我正在使用Forge API。 我需要执行PATCH请求。 当我使用Postman发送它时,一切都很好,但是当我使用HttpRequestMessage建立请求时,我得到一个响应-“ 415不支持的媒体类型”。 通过API文档Content-Type,我将其设置为“ application / vnd.api + json”。

邮递员的请求正文

enter image description here Headers of request in Postman

request object structure

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);
            }

2 个答案:

答案 0 :(得分:1)

使用"application/json"或使用HttpHeaders.TryAddWithoutValidation再试一次("application/vnd.api+json"HttpHeaders的内部验证中可能不合适):<​​/ p>

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");

答案 1 :(得分:0)

好的,我通过以这种方式设置ContentType解决了问题

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