Click here to Skip to main content
15,672,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear community,

I try to create an web api endpoint that returns an image in a format that can be defined in the 'Accept-Header' field of the request. I'm using Swashbuckle NugGet Package (Swashbuckle.AspNetCore, Version 6.5.0) and the UI shows the parameter correctly: see Swashbuckle Screenshot.
As you can see there too, the curl parameter -H 'accept: text/plain' is wrong and should be 'image/jpeg'.

The openAPI definition is specified with an
that removes Action parameter of a certain type and inserts the Header parameter instead (see code below).

I've no glue what I am doing wrong ...

Thank you for any hint, advice and maybe solution!

What I have tried:

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using PEAM.API.Model;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace PEAM.API.Middleware.Filter.OpenAPI
    public sealed class EaImageFormatFilter
        : IOperationFilter
        private const string PngMimeType = "image/png";
        private const string JpgMimeType = "image/jpeg";
        private const string BmpMimeType = "image/bmp";

        private const string OpenApiStringType = "string";
        private const string AcceptHeader = "Accept";

        private IList<IOpenApiAny> AcceptedImageTypes { get; }

        public EaImageFormatFilter()
            AcceptedImageTypes = new List<IOpenApiAny>()
                new OpenApiString(PngMimeType),
                new OpenApiString(JpgMimeType),
                new OpenApiString(BmpMimeType)
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
            var parameterType = typeof(EaImageFormat);
            var parameterToRemove = context.ApiDescription.ParameterDescriptions
                                        .Where(parameter => parameter.Type.Equals(parameterType))
                                        .Select(parameter => parameter.Name)

            if (parameterToRemove.Any())
                //--- parameter that are NOT of type EaImageFormat
                var keepParameter = (OpenApiParameter p) => !parameterToRemove.Contains(p.Name);

                //--- remove all parameters of type EaImageFormat
                operation.Parameters = operation.Parameters

                //--- add new parameter 'Accept' with location 'Header'
                var parameter = new OpenApiParameter
                    In          = ParameterLocation.Header,
                    Name        = AcceptHeader,
                    Description = "Pass accepted image format, e.g.: image/png, image/jpeg",
                    Required    = false,
                    Schema      = new OpenApiSchema
                        Type    = OpenApiStringType,
                        Default = new OpenApiString(PngMimeType),
                        Enum    = AcceptedImageTypes

BillWoodruff 15-Feb-23 21:56pm

stdout 16-Feb-23 1:53am    
Not sure what you want to tell me with the posted link ..? i know about this, but there is nothing that would help me. If I'm wrong, please show me more precisely what I missed. Thx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900