openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Fetch request is always sent with Accept: application/json header

Open iansan5653 opened this issue 5 years ago • 6 comments

Describe the bug I have an API that has some JSON endpoints and some file endpoints (media type application/octet-stream). The generated code works for the JSON endpoints, but for the file endpoints the server returns 406 because the code always sends the request with header Accept: application/json.

If there is no 200 response described with media type application/json, the generated code should use the first media type described or it should be a method parameter.

I'm using the Fetch API with version 0.7.0.

iansan5653 avatar Dec 10 '20 17:12 iansan5653

I was able to work around this by setting the HEADERS config to {Accept: ""}.

iansan5653 avatar Dec 10 '20 17:12 iansan5653

@iansan5653 good point, will fix this in the next mayor release

ferdikoomen avatar Dec 16 '20 18:12 ferdikoomen

@iansan5653 this work around is a bit risky, because the HEADERS are changed for every request, until you reset it. So if you have multiple requests running in parallel, this work around will cause errors!

@ferdikoomen so please consider a better solution (like an optional headers parameter to every generated service or something).

scipper avatar Feb 25 '21 07:02 scipper

@scipper I have not noticed any issues with this workaround - I just update the HEADERS property one single time at app startup and it works.

Although now I am using a different workaround - a custom request method using the undocumented --request parameter as described here https://github.com/ferdikoomen/openapi-typescript-codegen/issues/465#issuecomment-768278511. I copied over the default request method and modified it for my purposes.

iansan5653 avatar Feb 25 '21 15:02 iansan5653

@iansan5653 because an empty Accept header, as you configured it, will most likely be treated as Accept: */*. I did not think about that. So, as a work around, valid.

Thanks for the hint with the --request parameter. I am not sure if this helps me for the moment, but I will give it a try.

scipper avatar Feb 25 '21 15:02 scipper

@scipper There is a new version that allows to create client instances to each client can have its own bearer token. You can specify the --name argument while generating the client. More info: https://github.com/ferdikoomen/openapi-typescript-codegen#generate-client-instance-with---name-option

ferdikoomen avatar Jan 25 '22 15:01 ferdikoomen