Description
Use case:
I have server A that exposes "api A" to the UI (I develop/control server A)
I have server B that exposes "api B" (I do not develop/control server B)
"api A" calls "api B" and must send the user id in the header when calling "api B"
I use this open api generator in server A to call api B
The problem:
I cannot send user id in the header when calling api B, I thought of changing the global OpenAPI
config before every request, this won't work because I can get multiple requests at the same time , a global static object will not work here
Currently the only way to do this is to create a new client instance on every api call, this solution will work but might cause memory issues, especially because I have many client instances
Suggested solution:
if every generated function/api call had an optional header
parameter, I would be able send specific header on every call and won't have to create a new client instance
Activity
SCasarotto commentedon Jul 10, 2023
I have also run into this exact use case. I am using the generated code server side and I need to pass along a user based Authorization header with each request. I think each function accepting optional headers would be a good solution for this.
ansonallard commentedon Jul 21, 2023
You can add headers to your openapi spec (in the parameters section of the remote endpoint), and in doing so, this library will allow you to pass headers to api B's endpoint.
Openapi's docs:
An example:
Generated code:
JohnMusleh commentedon Jul 23, 2023
@ansonallard that works but I do not have control over the remote endpoint, I do not maintain it or have access to it so I can't change anything
in my issue I wrote:
"I have server B that exposes "api B" (I do not develop/control server B)"
optikalefx commentedon Nov 4, 2023
I agree this is a problem. It ends up making very verbose API calls when you have to pass all the headers as parameters to function calls.
For example, this is Amazon Marketing Cloud generated from their OpenAPI Spec
Even though we know the heades are
we have to pass this with every single API call. We can't even use a constructor and make instances of these classes. We just don't need such verbose API calls, we should be able to instantiate a client instance with some basic headers.
optikalefx commentedon Nov 5, 2023
I have made a PR that allows for headers defined at the client level to be the default values for params that openAPI says come from the header.
#1865
BnAmN commentedon Nov 15, 2023
I have come across a similar use case:
When using
Prism
as a mock server, it is necessary to specify aPrefer
HTTP header to select/query a specific sample (Prefer: 'example=Foo'
).This is only useful for development and testing, but I wouldn't include the header in the OpenAPI specification itself.
So for this case it would be extremely helpful to provide optional/additional HTTP headers.
I can think of even more useful cases for this, especially for development/testing purposes (e.g. overwriting default headers).
mrlubos commentedon Apr 7, 2024
Hey all, you could try using interceptors https://github.com/hey-api/openapi-ts
Let me know if that doesn't work for you!