question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Response "Content type" drop-down sets HTTP "Accept" header

See original GitHub issue

Using swagger-ui 3 and the petstore example, as deployed here: http://petstore.swagger.io.

For each operation, swagger-ui shows a list of response content types to choose from.

image

The list is populated from the “Produces” section in the OpenAPI (v2) specification file. For example:

"produces": [
        "application/json",
        "application/vnd.geo+json",
        "application/vnd.google-earth.kml+xml",
        "application/gml+xml",
        "application/text+csv"
    ],

Swagger-ui assumes that it should send an “Accept” header with a value of the selected content type with every request. This is not a good idea because not all APIs make use of the “Accept” header to specify the output format. For example, an API could instead use a query string parameter to specify output format. In that case, sending the “Accept” header is unnecessary, and the “Content Type” drop-down list is misleading because it doesn’t affect the response.

Ideally the OpenAPI Specification would provide a way to define how an API allows users to specify the output format. I’m not aware of a way in which OpenAPI v2 allows us to do this currently. Does anyone know if OpenAPI v3 will provide a way to do this?

In the mean time, can we have an option to remove the “Content Type” drop-down list (and its control of the “Accept” header) from swagger-ui?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
webroncommented, Jul 26, 2017

This is not going to be part of OAS3, and hopefully not a part of any other future versions. REST APIs are HTTP based, they use HTTP constructs. Content is negotiated using two headers - content-type for the payload, and accept for what the client expects to accept.

If you want to specify the content type using a query parameter, you’re welcome to do so, but the spec is not the right tool for the job. We’re not going to change the behavior. Of course, you’re welcome to write a plugin of your own to the system and change the behavior to suit your needs.

0reactions
DercilioFontescommented, Jun 6, 2019

I created a solution that worked fine for me.

Uncommeted the OperationFilter with a class to empty the produces and add my Accept header parameters. And inject a JS file to remove all "response-content-type" divs. Code below.

Without that, the Accept header gets a application/json before my Accept parameter value ("application/json; charset=utf-8; version=v3") be added, resulting in: "application/json, application/json; charset=utf-8; version=v3" or "application/json, charset=utf-8; version=v3" if I remove the application/json from the my default settings. It gets a comma after the default response content type application/json. Even I also using comma in my accept parameter it does not work,. That breaks the server to get my controller version parameter.

The solution remove completely the default response content type application/json to get only my Accept header parameter settings.

SwaggerConfig cs

AddRequiredHeaderParameters cs

customJS js

// SwaggerConfig.cs

GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {
                        c.OperationFilter<AddRequiredHeaderParameters>();

                    })
                    .EnableSwaggerUi(c =>
                    {
                        c.InjectJavaScript(thisAssembly, "Swashbuckle.Dummy.SwaggerExtensions.customJS.js");
                    });
// AddRequiredHeaderParameters.cs

public class AddRequiredHeaderParameters : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            operation.produces = new string[0];
            
            if (operation.parameters == null)
                operation.parameters = new List<Parameter>();

            operation.parameters.Add(new Parameter
            {
                name = "client_id",
                @in = "header",
                type = "string",
                required = true
            });
            operation.parameters.Add(new Parameter
            {
                name = "Authorization",
                @in = "header",
                type = "string",
                required = true
            });
            operation.parameters.Add(new Parameter
            {
                name = "Accept",
                @in = "header",
                type = "string",
                @default = "application/json; charset=utf-8; version=v3",
                required = true
            });
        }
    }
// customJS.js

const respContentTypeDivs = document.querySelectorAll("div.response-content-type");
respContentTypeDivs.forEach(e => e.parentNode.removeChild(e));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Accept - HTTP - MDN Web Docs
The Accept request HTTP header indicates which content types, expressed as MIME types, the client is able to understand.
Read more >
Accept Header Field - Software AG Documentation
The Accept header field specifies which content type or types the client will accept in the response. The Accept header field can specify...
Read more >
Difference Between 'Accept' and 'Content-Type' HTTP Headers
“Accept” is a request header and “Content-Type” is both request and response header. Let's see the difference between these headers:.
Read more >
application/xml selected by default in Response content type ...
... site ( http://localhost/swagger-ui.html#/ ) application/xml is always selected by default in the "Response content type" dropdown.
Read more >
HTTP/1.1: Header Field Definitions
The Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found