Skip to content

FastAPI integration ROOT_PATH problem #1832

Closed
@sector119

Description

@sector119

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.12.0

Steps to Reproduce

I have FastAPI app with nginx as proxy with following config. I host my api service on "/api/xmlrpc/v1" location and I rewrite it to send to my app on "/" location.

location /api/xmlrpc/v1 {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass http://xmlrpc_api_service_upstream;
    rewrite ^/api/xmlrpc/v1/(.*)$ /$1 break;
}

To get FastAPI docs to work I must set FastAPI root_path argument to "/api/xmlrpc/v1" when I create app.

Here is request scope example
{'type': 'http', 'scheme': 'https', **'root_path': '/api/xmlrpc/v1'**, **'path': '/api/xmlrpc/v1'**}

Expected Result

What can I do to get url tag and curl example urls look as expected (https://api.my.site/api/xmlrpc/v1) ?

Actual Result

I get url tag like https://api.my.site/api/xmlrpc/v1/api/xmlrpc/v1 not https://api.my.site/api/xmlrpc/v1 as it has to be, as well as curl example.

api

That's happened because of asgi.py integration's _get_url() function where url path is
scope.get("root_path", "") + scope.get("path", "") that is why it got duplicated.

I can workaround it by setting FastAPI root_path=None and openapi_url='/api/xmlrpc/v1/openapi.json' and don't rewrite location for '/api/xmlrpc/v1/openapi.json' location while sending request to app from nginx, but it looks like a dirty hack.

Thank You

Metadata

Metadata

Assignees

Type

Projects

Status

Waiting for: Community

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions