Skip to content

Add better support for request body access/manipulation - #589

Merged
tiangolo merged 6 commits into
fastapi:masterfrom
dmontagu:app-override
Oct 5, 2019
Merged

tiangolo merged 6 commits into
fastapi:masterfrom
dmontagu:app-override

Conversation

@dmontagu

@dmontagu dmontagu commented Oct 4, 2019 •

Copy link
Copy Markdown
Contributor

This pull request introduces a small change to APIRoute to make it easier to construct a custom app attribute in a custom subclass.

There is a lot of custom functionality that this change would simplify, but there are two especially important capabilities it adds:

  • A way to get handle exceptions with access to the request body
  • A way to modify the request body prior to its parsing/handling by FastAPI

I've added two sections to the "Using the Request Directly" docs showing how to use the changes in this PR to implement both of the above kinds of feature.


There have been many recent github issues that could be better-addressed with this capability.

Related to accessing the body in middleware/exception handlers:

Related to modifying the request prior to handling by FastAPI:


Quoting Tom Christie from Kludex/starlette#495:

"Consume request body in middleware is problematic"

Indeed. Consuming request data in middleware is problematic.
Not just to Starlette, but generally, everywhere.

On the whole you should avoid doing so if at all possible.

I think using middleware to consume the request body is the obvious (or, perhaps, "naive") way to handle these issues. But that approach is explicitly discouraged, and not supported. So I think the approach described in / supported by this PR is especially valuable.


(This pull request is in lieu of #568, and includes what I think is both the smallest and most important change to APIRoute from that pull request.)

@codecov

codecov Bot commented Oct 4, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #589 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #589    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files         258    264     +6     
  Lines        6307   6427   +120     
======================================
+ Hits         6307   6427   +120
Impacted Files Coverage ฮ”
.../test_custom_request_and_route/test_tutorial003.py 100% <100%> (รธ)
docs/src/custom_request_and_route/tutorial002.py 100% <100%> (รธ)
docs/src/custom_request_and_route/tutorial001.py 100% <100%> (รธ)
.../test_custom_request_and_route/test_tutorial002.py 100% <100%> (รธ)
.../test_custom_request_and_route/test_tutorial001.py 100% <100%> (รธ)
fastapi/routing.py 100% <100%> (รธ) โฌ†๏ธ
docs/src/custom_request_and_route/tutorial003.py 100% <100%> (รธ)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
ฮ” = absolute <relative> (impact), รธ = not affected, ? = missing data
Powered by Codecov. Last update 7a504a7...b2640d3. Read the comment docs.

@dmontagu dmontagu changed the title Simplify use of custom Request behavior Add better support for request body access/manipulation Oct 4, 2019
Comment thread docs/tutorial/using-request-directly.md Resolved
@tiangolo
tiangolo merged commit 8c3ef76 into fastapi:master Oct 5, 2019
@tiangolo

tiangolo commented Oct 5, 2019

Copy link
Copy Markdown
Member

Awesome @dmontagu ! Thanks a lot! ๐ŸŽ‰ ๐Ÿš€ ๐Ÿฐ

I think this is sufficiently important to have its own section in the docs, so I moved it. I put it near the end as I consider this quite advanced and it could confuse newcomers.

I also added an extra example setting the route_class in a router. For that, I had to rebase from master, to include your recent fix ๐Ÿ˜…

Thanks again! ๐Ÿ™‡โ€โ™‚๏ธ ๐ŸŒฎ

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

Successfully merging this pull request may close these issues.

2 participants