Is it possible to get the headers from the request?
See original GitHub issueHello!
First of all, thank you so much for making Graphene and Graphene Django, they’re incredible.
Currently I’m trying to implement an authentication system, my idea was to return a JWT on a successful login and then send the token in a Authorization
header, however, I don’t have any idea on how to access the request headers (or even if it’s possible).
Thank you so much once again 👏
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:13 (1 by maintainers)
Top Results From Across the Web
Request.headers - Web APIs | MDN
The headers read-only property of the Request interface contains the Headers object associated with the request.
Read more >How To Get HTTP Request Header In Java - Mkyong.com
1.1 Loop over the request header's name and print out its value. WebUtils.java. package com.mkyong.web.utils; import javax.servlet.http.
Read more >Is it possible to get request headers from one ... - Stack Overflow
Yes. Have you right-clicked on the request copied as fetch to see how that's formatted? It won't be an automatic process, but you...
Read more >Is it possible to read and extract HTTP request headers via ...
I just need to be able to read the X-CSRF-TOKEN HTTP request header that is set in the HTTP request (not response), extract...
Read more >getallheaders - Manual - PHP
Beware that RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. Therefore, array keys of getallheaders() should be converted first to lower- ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Oxyrus you can access request and its headers by using context field from info parameter in your resolve methods:
However, for authentication purposes, you’d have to do that for all of your queries and mutations. Instead of that, you can define a custom view for your
/graphql
endpoint like this:And use this view in your urls.py. You need to define
valid_header
function that will do actual validation.Let me know if this helps.
@Oxyrus that’s correct.
In this case, you can implement your own Middleware (http://docs.graphene-python.org/en/latest/execution/middleware/) and have different rules for different fields in your schema.
Other option would be to have two endpoints. One for non-logged users served by GraphQLView with query containing fields that should be exposed to everyone. And another endpoint served by TokenAuthGraphQLView with queries and mutations available to logged in users. I like it as it makes it easier to distinguish between public and private APIs, but GraphQL specification recommends having only one endpoint, so it might not follow the specification fully.
The third option would be to authenticate non-logged users as guests and provide access to certain fields based on your authorization rules and framework, but that’s basically the same as having authentication checks in all fields.