No "Try it out" button for HEAD method in Swagger UI
Asked Answered
T

3

5

I have a Swagger spec that defines a HEAD operation:

head:
  description: show flight exist or not.

  parameters:
    - name: flight_no
      in: path
      type: string
      description: Flight_no
      required: true
  produces:
    - application/json
    - application/xml

  responses:
    200:
      description: success response
      schema:
        type: array
        items:
          $ref: '#/definitions/Data'
    '404':
      description: flight does not exist

In Swagger UI v. 2, this HEAD operation has no "try it out" button. How can I add "try it out" for HEAD?

HEAD operation in Swagger UI

Tiddlywinks answered 20/3, 2017 at 12:52 Comment(0)
M
6

You can try Swagger UI 3.0 – HEAD has "try it out" by default in this version.

If you use Swagger UI 2.0, HEAD is disabled by default. You need to explicitly enable it in the supportedSubmitMethods list in the Swagger UI initialization code in index.html:

window.swaggerUi = new SwaggerUi({
  url: url,
  dom_id: "swagger-ui-container",
  supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch', 'head'],
  //                                                                   ^
  // ------------------------------------------------------------------┘


By the way, schema in a HEAD response is not really useful because HEAD is not supposed to return an actual body – only the headers. You should change the 200 response to:

responses:
  200:
    description: success response
Meaty answered 24/3, 2017 at 22:8 Comment(0)
B
2

to add up, in java you can add a bean in your swagger config class to do that:

@Configuration
@EnableSwagger2
public class SwaggerConfig { 
                               
    @Bean
    UiConfiguration uiConfig() {
      return new UiConfiguration(null, UiConfiguration.Constants.NO_SUBMIT_METHODS);
    }
}

Basically it will put supportedSubmitMethods to [].

Baggott answered 15/8, 2018 at 15:54 Comment(0)
F
1

You need to extend DEFAULT_SUBMIT_METHODS by "head" value.

    @EnableSwagger2
    @Configuration
    public class SwaggerConfig  {

        @Bean
        UiConfiguration uiConfiguration() {
            return new UiConfiguration( null, new String[] {"get", "post", "put", "delete", "patch", "head"} );
        }
    }
Fluctuant answered 20/4, 2019 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.