我遇到了一個奇怪的問題,我的一個 API 端點415 Content type not supported在發出 GET 請求時失敗了,我正在努力找出原因。
更奇怪的是它在 Postman 中運行良好。
它與我需要做的 URL 重寫有關,因此我可以將請求路由到適當的服務器。如果我不進行重寫,它也可以。
重定向代碼非常基本,它只是獲取請求的身份并獲取服務器的地址。對于背景關系,我需要根據 GDPR 合規性的請求設定路由到不同的服務器。如果有人有更好的方法來做到這一點,我會全力以赴。
public class ProxyHandler : DelegatingHandler
{
private async Task<HttpResponseMessage> RedirectRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var identity = await GetIdentityAsync(request) as ParsedIdentity;
using (var client = new HttpClient())
{
var clonedRequest = await request.CloneAsync();
clonedRequest.RequestUri = new Uri(identity.Server.ApiUri request.RequestUri.PathAndQuery);
return await client.SendAsync(clonedRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
}
}
///Clone Async Extension
public static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessage req)
{
HttpRequestMessage clone = new HttpRequestMessage(req.Method, req.RequestUri);
if (req.Content.Headers.ContentLength != null && req.Content.Headers.ContentLength > 0)
{
var contentStream = new StreamReader(await req.Content.ReadAsStreamAsync().ConfigureAwait(false));
contentStream.BaseStream.Seek(0, SeekOrigin.Begin);
clone.Content = new StreamContent(contentStream.BaseStream);
// Copy the content headers
if (req.Content.Headers != null)
foreach (var h in req.Content.Headers)
clone.Content.Headers.Add(h.Key, h.Value);
}
clone.Version = req.Version;
foreach (KeyValuePair<string, object> prop in req.Properties)
clone.Properties.Add(prop);
foreach (KeyValuePair<string, IEnumerable<string>> header in req.Headers)
clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
return clone;
}
我已經逐步完成了代碼并觀察到它到達了預期的服務器,進行了身份驗證,并檢查了 RouteData 以確保它是正確的控制器和方法。我已檢查以確保該方法是 GET 并且沒有請求正文。這一切都是正確的。但是,一旦它將請求交給控制器,它就不會到達并回傳415 Content type not supported
我什至完全重新創建了一個新請求,而不是克隆它(對于這個特定的請求),它仍然會這樣做。
更奇怪的是,到目前為止,我只確定了一個這樣做的請求。其他所有 GET、POST、PUT、PATCH、DELETE 都有效。正是這一請求失敗了。它快把我逼瘋了。特別是它在郵遞員中作業。
感謝任何幫助,我花了太多時間試圖解決這個問題。
Request
{
"Method": "GET",
"Url": "http://localhost:4201/site/en-us/menus/7aa17e5d-d500-4ee7-889e-66cad2f3057e?menuTypes=all,main-nav",
"ServerVariables": "HTTP/1.1",
"RequestGuid": "bc5a4bfb-09f4-4e7d-bee1-19774d6aae6d",
"header_Connection": "Close",
"header_Accept": "application/json",
"header_Accept-Encoding": "gzip, deflate, br",
"header_Accept-Language": "en-US, en; q=0.9",
"header_Authorization": "my-token",
"header_Host": "localhost:44358",
"header_Referer": "http://localhost:4200/",
"header_User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44",
"header_sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Microsoft Edge\";v=\"100\"",
"header_sec-ch-ua-mobile": "?0",
"header_sec-ch-ua-platform": "\"Windows\"",
"header_origin": "http://localhost:4200",
"header_sec-fetch-site": "cross-site",
"header_sec-fetch-mode": "cors",
"header_sec-fetch-dest": "empty",
"_env": "local"
}```
uj5u.com熱心網友回復:
我能夠解決這個問題,但是我仍然不確定問題到底是什么,所以如果有人能回答,將不勝感激。
我想指出,這個 API 已經在生產系統中使用了大約 3 年而沒有重寫 URL,并且可以在瀏覽器或 Postman 中正常作業。此問題僅在通過代理來自瀏覽器時出現。
為了解決這個問題,我改變了:
public async Task<HttpResponseMessage> GetMenus(Guid siteFirmUuid, string[] menuTypes, string locale = "en-us")
到:
public async Task<HttpResponseMessage> GetMenus(Guid siteFirmUuid, [FromUri]string[] menuTypes, string locale = "en-us")
我知道引數系結是如何作業的,但我只是不明白為什么它在 Postman 中作業,在直接從站點呼叫時作業,但在缺少 FromUri 屬性時從通過代理的站點失敗。
在這一點上,我很高興它已修復,但我非常感興趣是否有人可以幫助我弄清楚為什么這只在我上面描述的特定實體中失敗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462430.html
標籤:C# 网 有角度的 asp.net-web-api
