Chidambaranathan M
Chidambaranathan M

Reputation: 131

base_url issues in drf_yasg swagger

I am running Django with Nginx in 8000 port. Here Nginx is exposed outside in 8000 port. But in Swagger UI the base_url is set as without port. So Swagger is trying to access endpoints without a port, then it gives a response as TypeError: Failed to fetch.

Kindly refer to the below image to know how base_url is set enter image description here

Kindly refer to the below image to know how a request is sent from Swagger enter image description here

When I access the endpoint (with port) using Postman it is working fine. The problem occurs only when I try using Swagger (because is sent a request without port).

Here I want Swagger to sent all the requests with the port. Please help me to resolve this issue. Thanks.

Note:

  1. I'm using the drf-yasg==1.17.1 package to generate Swagger UI
  2. Other requirements are Django==3.0.3 and djangorestframework==3.11.0

Upvotes: 3

Views: 3973

Answers (4)

Okot Daniel
Okot Daniel

Reputation: 21

enter image description hereI personally think its something to do with Https or Http request, i ran into the same issue a couple of days ago and for me the simple solution was to change the scheme from http when making requests using my local set up and to https ( on live server ) since i had installed ssl and only secure traffic was allowed.

Upvotes: 0

Chidambaranathan M
Chidambaranathan M

Reputation: 131

Thanks, @Petr Synek for your answer

We need to tell the Nginx to forward the http_host.

location / {
     proxy_set_header Host $http_host;
}

This one resolved the above-mentioned issue

Upvotes: 2

Petr Synek
Petr Synek

Reputation: 323

You need to tell the Nginx to forward the server port as well. As swager gets the base url port from request header (if not hardcoded as mtshaikh proposed).

eg. something like this this should be in Nginx config

location / {
            proxy_set_header Host $host:$server_port;
    proxy_pass http://example.com:8000/;
}

Upvotes: 7

mtshaikh
mtshaikh

Reputation: 688

Not a good solution but defining the complete url in the get_schema_view() in urls.py works!

schema_view = get_schema_view(
    openapi.Info( ... ),
    url='http://example.net:8080/', # Important bit
    public=True,
    permission_classes=(permissions.AllowAny,)
)

Source

Upvotes: 1

Related Questions