Validating Request Content-Type

Alexandre Vicenzi

I'm trying to validate the value of Content-Type in POST, PUT and PATCH requests, but the current code is only working when I forget the content-type clause or when I use a content-type like: "Content-Type: Foo".
When I send "Content-Type: text/css" I get this:

500 Internal Server Error
No MediaTypeFormatter is available to read an object of type 'MyClassDto' from content with media type 'text/css'.

This is my code:

public class ContentTypeFilter : IActionFilter
{
    private readonly List<MediaTypeHeaderValue> _suport;

    /// <summary />
    public ContentTypeFilterAttribute()
    {
        _suport = new List<MediaTypeHeaderValue>();

        foreach (var formatter in GlobalConfiguration.Configuration.Formatters.ToArray())
        {
            _suport.AddRange(formatter.SupportedMediaTypes);
        }
    }

    public bool AllowMultiple { get { return false; } }

    public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
        var metodos = new List<string> { "POST", "PUT", "PATCH" };

        if (actionContext.Request.Content != null)
        {
            if (metodos.Contains(actionContext.Request.Method.Method.ToUpperInvariant()))
            {
                MediaTypeHeaderValue contentType = actionContext.Request.Content.Headers.ContentType;

                if (contentType == null || !_suport.Any(x => x.MediaType.Equals(contentType.MediaType)))
                {
                    return CreateResponse(actionContext.Request, "Invalid Content-Type");
                }
            }
        }

        return continuation();
    }

    private static Task<HttpResponseMessage> CreateResponse(HttpRequestMessage request, string mensagem)
    {
        var tsc = new TaskCompletionSource<HttpResponseMessage>();
        var response = request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
        response.ReasonPhrase = mensagem;
        response.Content = new StringContent(mensagem);
        tsc.SetResult(response);

        return tsc.Task;
    }

Is there another way to validate content-type and return error 415 if the content isn't XML or JSON?

Alexandre Vicenzi

I've found a good solution here.

With some changes to get what I want:

public class ContentTypeFilter : DelegatingHandler
{
    private readonly List<MediaTypeHeaderValue> _suport;

    /// <summary />
    public ContentTypeFilter()
    {
        _suport = new List<MediaTypeHeaderValue>();

        foreach (var formatter in GlobalConfiguration.Configuration.Formatters.ToArray())
        {
            _suport.AddRange(formatter.SupportedMediaTypes);
        }
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var metodos = new List<string> { "POST", "PUT", "PATCH" };

        if (request.Content != null)
        {
            if (metodos.Contains(request.Method.Method.ToUpperInvariant()))
            {
                MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;

                // Nas configurações não possui o Charset aceito.
                if (contentType == null || !_suport.Any(x => x.MediaType.Equals(contentType.MediaType)))
                {
                    return Task<HttpResponseMessage>.Factory.StartNew(() => CreateResponse(request, "Suported content-types: " + string.Join(", ", _suport.Select(x => x.ToString()))));
                }
            }
        }
        return base.SendAsync(request, cancellationToken);
    }

    private static HttpResponseMessage CreateResponse(HttpRequestMessage request, string mensagem)
    {
        var response = request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
        response.ReasonPhrase = mensagem;
        response.Content = new StringContent(mensagem);

        return response;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NodeJS : Validating request type (checking for JSON or HTML)

From Dev

symfony request Acceptable content type

From Dev

Validating a part of request in DataPower

From Dev

Validating a part of request in DataPower

From Dev

Validating array request in Laravel

From Dev

Set request Content-Type on WinRT HttpClient

From Java

ZIP file content type for HTTP request

From Dev

How to determine Content Type of a HTTP Servlet Request?

From Dev

Post request to include 'Content-Type' and JSON

From Dev

Web request Content type is always text/html

From Dev

WCF Post Request content type text/xml

From Dev

Check Content-type POST request PHP

From Dev

Setting response content type header in JSONP request?

From Dev

Content-Type - WebAPI - Request Header

From Dev

.NET HttpClient Request Content-Type

From Dev

HTTP Response content type different on HEAD request

From Dev

Setting response content type header in JSONP request?

From Dev

How to determine Content Type of a HTTP Servlet Request?

From Dev

Web request Content type is always text/html

From Dev

Override content-type for HEAD request

From Dev

JAX-RS validating Content-Type and Accept header for generic upload and download API

From Dev

Validating primitive data type

From Dev

Validating a Data Type in Python

From Dev

Validating request parameter before controller

From Dev

validating SOAP-Request with a SOAPHandler

From Dev

Validating request parameter before controller

From Dev

Validating Request Schema With Fastify and AJV

From Dev

Failed validating 'type' json schema

From Dev

Validating String and Type Conversion in JavaScript

Related Related

HotTag

Archive