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

How to set Redirect response #297

Closed
KStenK opened this issue Feb 14, 2021 · 8 comments
Closed

How to set Redirect response #297

KStenK opened this issue Feb 14, 2021 · 8 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@KStenK
Copy link

KStenK commented Feb 14, 2021

Hello,

I'm using DRF APIView and I want to show 301 redirects and redirect location in swagger-ui.

I can add a status code like this:

@extend_schema(responses={301: None})

But how can I set the response header and add the header: Location: /.

I thought i can do something like this:

@extend_schema(responses={301: {"headers": {"Location": "/"}}

To get schema something like this:

responses:
  '301':
    headers:
      Location: /

But that does not work.

Is there any way I can do that or how should I customize responses to set headers/cookies with extend_schema?

@tfranzel
Copy link
Owner

@KStenK that feature is not implemented yet. there is currently no direct access for this. we will definitely add this

@sergei-maertens has volunteered to implement this feature in #283. are you still working on this?

@sergei-maertens
Copy link
Contributor

It's still on my todo list, but I've been swamped with more urgent things. I'll plan some time at the end of the day to get a PR going!

@tfranzel
Copy link
Owner

@KStenK feature is now available. see here for usage:

OpenApiParameter(
name='Location',
type=OpenApiTypes.URI,
location=OpenApiParameter.HEADER,
description='URL of the created resource',
response=[201],
),

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Feb 17, 2021
@KStenK
Copy link
Author

KStenK commented Feb 17, 2021

I don't know what I do wrong, but for me, it does not work.

Here is what have I tried:

@extend_schema(
  responses={301: None},
  tags=["Registration"],
  parameters=[
      OpenApiParameter(
          name="Location",
          type=OpenApiTypes.URI,
          location=OpenApiParameter.HEADER,
          description="/",
          response=[301]
      )
  ])

Schema generated:

    get:
       . . .
      responses:
        '301':
          description: No response body

Swagger UI:
image

@tfranzel tfranzel added bug Something isn't working and removed fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Feb 17, 2021
@tfranzel
Copy link
Owner

@KStenK but now it should work. we did not account for the empty body case.

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Feb 17, 2021
@KStenK
Copy link
Author

KStenK commented Feb 17, 2021

Now looks fine for me :)
image

@KStenK KStenK closed this as completed Feb 17, 2021
@KStenK
Copy link
Author

KStenK commented Feb 17, 2021

Is there a new version with that feature out too at pip soon?

@tfranzel
Copy link
Owner

i have to fix 2 other issues first. next release is probably next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

3 participants