1

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?

Karthik Subramaniyam
  • 1,141
  • 2
  • 8
  • 15

1 Answers1

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