Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

about the header parameter #25

Closed
larssonsun opened this issue Oct 30, 2018 · 8 comments
Closed

about the header parameter #25

larssonsun opened this issue Oct 30, 2018 · 8 comments

Comments

@larssonsun
Copy link

larssonsun commented Oct 30, 2018

hi, thanks for your proj:
how to add a parameter whitch the parameter's type is header,for exmaple
the accept header or some customize headers juest like token ?

@maximdanilchenko
Copy link
Owner

Hi!
There are several ways to do so.

  1. If you need it for authentication - use securityDefinitions swagger param, like so:
setup_aiohttp_apispec(
        app,
        title='My API',
        version=__version__,
        info={"description": "some markdown description"},
        securityDefinitions={
            "user": {"type": "apiKey", "name": "Authorization", "in": "header"}
        },
)

It will add Authorization header in your documentation auth. Read more about it here. After that you can read headers from request.headers multidict.
2. If you need to add headers to some handler - use locations parameter, like so:

@use_kwargs(MyRequestSchema(strict=True), locations=['headers'])
async def my_handler(self, request: web.Request) -> web.Response:
    return web.Response()

After that you will see it in your documentation exactly for this method.
But as for now this way works not correctly with aiohttp_apispec.validation_middleware. It will update request['data'] dict with those headers. It can broke something if you have equal param names in your content and headers. So it is not recommended way. I want to fix this behavior someday.

@larssonsun
Copy link
Author

thank you for your reply:
i just want to add api version into accpect header,
some like this:
Accept: vnd.example-com.foo+json; version=1.0

@maximdanilchenko
Copy link
Owner

setup_aiohttp_apispec(
        app,
        securityDefinitions={
            "user": {"type": "apiKey", "name": "Accept", "in": "header"}
        },
)

@larssonsun
Copy link
Author

larssonsun commented Oct 31, 2018

setup_aiohttp_apispec(
        app,
        securityDefinitions={
            "user": {"type": "apiKey", "name": "Accept", "in": "header"}
        },
)

sorry .but this solution can not make the request from swagger contain the accept header just like :
Accept: text/html; version=0.9

@larssonsun
Copy link
Author

larssonsun commented Oct 31, 2018

There are some extra consultations.
How to edit or add some comments and how to edit the parameter names with aiohttp-apispec?
2
1
💯

@maximdanilchenko
Copy link
Owner

Field description - use description argument in field.
Parameter name is the actual name of your parameter. You named it Version in your code, so the name in the docs will be Version.
If you want to add some text outside of methods description, you can use markdown like in my first comment in info["description"]

@maximdanilchenko
Copy link
Owner

@larssonsun I see that the question is answered, so I'm closing issue.

@khab-el
Copy link

khab-el commented Dec 26, 2021

setup_aiohttp_apispec(
        app,
        securityDefinitions={
            "user": {"type": "apiKey", "name": "Accept", "in": "header"}
        },
)

sorry .but this solution can not make the request from swagger contain the accept header just like : Accept: text/html; version=0.9

if you still need this feature, you have to add

setup_aiohttp_apispec(
        app,
        securityDefinitions={
            "user": {"type": "apiKey", "name": "Accept", "in": "header"}
         },
        security=[{"user": []}]
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants