I created a azure HTTP triggered function app using python which accepts request and return response based on the request parameters. Now I want read the cookies from the request. How to read the cookie from request?
Asked
Active
Viewed 2,273 times
1
-
Is this using Azure Functions V1 or V2? The python support is very different (and improved) in V2. – Connor McMahon Jun 05 '19 at 21:40
-
Yes. I'm using version 2. @ConnorMcMahon. – Karthik Subramaniyam Jun 06 '19 at 09:05
1 Answers
1
You would just have to load/parse the Cookie
header from the req
object using http.cookies.SimpleCookie
from http.cookies import SimpleCookie
import azure.functions as func
import logging
def main(req: func.HttpRequest) -> func.HttpResponse:
cookie = SimpleCookie()
cookie.load(req.headers['Cookie'])
return func.HttpResponse(f"{cookie['username'].value}")
To test this code, send a request with header like this
Cookie: username=oreo

PramodValavala
- 6,026
- 1
- 11
- 30
-
Is there a list of all available req headers? I need hostname but I can't find the python syntax/list of available headers. – ericOnline Oct 21 '20 at 23:10
-
HTTP Headers are populated into a dictionary. Any [HTTP Header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) passed in the request should be available. Hostname would be in the [Host Header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host). – PramodValavala Oct 22 '20 at 05:18