question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Questions about middlewares

See original GitHub issue

I’d like to do a very simple task, that is add a middleware which runs before the endpoints and adds a few properties to the request object. Very similar to how authentication middleware does it, but I’d like to make it more flexible.

My aim is to have something like this:

  • request.user_.user_data
  • request.user_.org_data
  • request.user_.allowed_regions for example

Now, my problem is the following:

  • BaseHTTPMiddleware in the docs starts with response = await call_next(request) and it adds a Header after the request has been processed. How can I use BaseHTTPMiddleware to add processing before an endpoint runs?
  • Can I add custom properties to the request object in a Middleware?
  • In case I cannot do it with BaseHTTPMiddleware I’m confused about the pure ASGI 2 vs. 3. middlewares. All the examples in starlette 0.11.4 are on ASGI 2, but ProxyHeadersMiddleware from uvicorn github is ASGI 3, do I understand it right?
  • I get a INFO: ASGI 'lifespan' protocol appears unsupported. if I try to add something based on the skeleton of ProxyHeadersMiddleware. Will it disappear if I upgrade to 0.12.beta?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
hyperknotcommented, Apr 24, 2019

OK, I’ve been able to make some progress trying to write my ASGI middleware for 0.12.

class JSONAuthMiddleware:
    def __init__(self, app):
        self.app = app

    async def __call__(self, scope, receive, send):
        if scope['type'] != 'http':
            response = PlainTextResponse('non http', status_code=400)
            await response(scope, receive, send)

        if scope['method'] != 'POST':
            response = PlainTextResponse('non POST', status_code=400)
            await response(scope, receive, send)

        request = Request(scope, receive=receive)
        data = await request.json()

        await self.app(scope, receive, send)

My problem is that if I add a request.json() line in the middleware, then it cannot be called in the view. When the view calls it, it stalls the app and uvicorn need to be force quitted (Waiting for connections to close. (CTRL+C to force quit)).

How can I solve this (or maybe this is a bug?).

0reactions
tomchristiecommented, May 20, 2019

There’s a bunch of different things being asked here, so I’m going to close this off as a general discussion rather than a narrowed-down actionable requested change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

20 Middleware Interview Questions and Answers - CLIMB
1. What is a middleware? · 2. Can you give me some examples of middleware in the context of web development? · 3....
Read more >
26 Middleware Interview Questions and Answers
26 Middleware Questions and Answers: ; 1 :: What is garbage collection and what is it used for? ; 2 :: What is...
Read more >
Top 13 Layering & Middleware Interview Questions And ...
Top 13 Layering & Middleware Interview Questions · eustjWhy is it a good idea for “lower” application layers not to be aware of...
Read more >
14 questions with answers in MIDDLEWARE | Science topic
Review and cite MIDDLEWARE protocol, troubleshooting and other methodology information | Contact experts in MIDDLEWARE to get answers.
Read more >
Middleware Administrator Interview Questions - AmbitionBox
Middleware Administrator Interview Questions · Q1. How to implement circular and linear logging in WMQ? · Q2. How to create and configure Queue ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found