Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending dynamic custom headers in swagger UI try outs

I am using swagger in java.

I am reading a header called callerId through requestAttributes in the code. I am not using the header through the annotation @HeaderParam.

because of this reason, the header section is not showing up in the swagger UI for try outs.

How can I make this header show up in the try out form.

Is there any way I can achieve this without hard coding the header value.

Thanks in advance.

like image 212
Sharath B. Patel Avatar asked Nov 04 '14 18:11

Sharath B. Patel


People also ask

How do you send Authorization header with request in swagger UI?

After add settings, then run this project, you can find an Authorization button swagger page, and you can use it to set the authorization header. Show activity on this post. then in SwaggerUIBundle constructor: const ui = SwaggerUIBundle({ ..., requestInterceptor: function (req) { req.

Can we pass header in swagger?

In ASP.NET Web API, the simplest way to pass-in a header on Swagger UI is to implement the Apply(...) method on the IOperationFilter interface.

How do I pass multiple headers in swagger?

You can pass multiple header params on a single request, just use unique names for them (key is used in the above example). To be clear, both headers are added to Access-Control-Allow-Headers, and both can be recived separatelly by server, the case is that i'm not able to send them together.

How to add custom header in Swagger?

HI there, If you are customizing the index.html page then you can add your own header by changing the addApiKeyAuthorization javascript function in the index.html page and by adding below properties in the SwaggerConfig.cs class. SwaggerConfig.cs

How do I create a custom layout in Swagger UI?

You can specify a different layout to be used by passing the layout's name as the layout parameter to Swagger UI. Be sure to provide your custom layout as a component to Swagger UI. For example, if you wanted to create a custom layout that only displayed operations, you could define an OperationsLayout:

Is there a way to send parameters from a custom header?

Also it's possible to use attribute [FromHeader] for web methods parameters (or properties in a Model class) which should be sent in custom headers. Something like this: [HttpGet] public ActionResult Products ( [FromHeader (Name = "User-Identity")] string userIdentity) At least it works fine for ASP.NET Core 2.1 and Swashbuckle.AspNetCore 2.5.0.

What are the response headers of an API request?

Responses from an API can include custom headers to provide additional information on the result of an API call. For example, a rate-limited API may provide the rate limit status via response headers as follows: { ... } summary: Checks if the server is alive. description: Request limit per hour.


1 Answers

You can add parameters to an operation using @ApiImplicitParams and @ApiImplicitParam.

The following wiki documentation should provide you with an explanation on how to use these annotations - https://github.com/swagger-api/swagger-core/wiki/Annotations#apiimplicitparam-apiimplicitparams

Keep in mind I assume here that this is not a security header but actually part of the API (at least based on the description you gave). If you're looking for a security header, there's an alternative method. Please leave a comment and I'll edit this response.

like image 152
Ron Avatar answered Oct 10 '22 16:10

Ron