'Change request Content-Type to allow invalid/unsupported request Content-Types
A client is currently sending our ASP.NET 4.x/Core REST APIs the correct request payload but invalid request Content-Type which is resulting in 415 status codes being returned. Obviously the client sending the invalid Content-Type is an issue but I'd like to consider a server side fix as an interim solution.
One solution which seems to resolve the issue is to modify the request Content-Type in the middleware as an example shown below:
public async Task Invoke(HttpContext context)
{
if (context.Request.Headers["Content-Type"] == "invalid json header")
{
context.Request.Headers["Content-Type"] = "application/json";
}
}
However I'm wondering if there's a better/more elegant solution that doesn't involve adding extra middleware?
Edit: another potential solution is to add application/octet-stream to JsonFormatter.SupportedMediaTypes which is what the Content-Type defaults to in cases where its omitted/invalid but I think this will force application/octet-stream requests to read as application/json requests which isn't ideal either as we do get media upload requests with application/octet-stream as Content-Type
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
