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.

Can not read cookies

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import uvicorn
from typing import Optional
from fastapi import FastAPI, Response, Cookie

app = FastAPI()
@app.get("/set")
async def create_cookie(response: Response):
    response.set_cookie(key="fakesession", value="fake-cookie-session-value")
    return {"message": "Come to the dark side, we have cookies"}

@app.get("/read")
async def read_items(ads_id: Optional[str] = Cookie(None)):
    return {"ads_id": ads_id}

if __name__ == "__main__":
    uvicorn.run(app, port=80)

Description

When access /read ,I got {"ads_id": null}

Operating System

Windows

Operating System Details

Winodws 11

FastAPI Version

0.70.0

Python Version

Python 3.9.7

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
mattholycommented, Oct 18, 2021

@libra146 Additionally if your cookie’s key is something like X-Token (with a dash in it), you need to read it as x_token

3reactions
mattholycommented, Oct 18, 2021

@mattholy I also encountered this problem, can you provide some help, thank you

Your variable’s name must be the same as your cookie’s key e.g. in the example I provided above if you would like to read fakesession in cookies you have to make your code be like :

async def read_items(fakesession: Optional[str] = Cookie(None)):
    return {"whatever": fakesession}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't access cookies from document.cookie in JS, but browser ...
You are most likely dealing with httponly cookies. httponly is a flag you can set on cookies meaning they can not be accessed...
Read more >
Turn cookies on or off - Computer - Google Account Help
On your computer, open Chrome. · At the top right, click More More and then Settings. · Under "Privacy and security," click Site...
Read more >
Enable cookies in your web browser
Click "Site Settings". Click "Cookies and site data". In the Privacy and security section, click Content Settings. Click the slider to "Allow ...
Read more >
7 Keys to the Mystery of a Missing Cookie
7 Keys to the Mystery of a Missing Cookie · 1. SameSite attribute Defaults to Lax · 2. withCredentials is not Set to...
Read more >
Cookies, document.cookie - The Modern JavaScript Tutorial
If you run it, then probably you'll see multiple cookies. That's because the document.cookie= operation does not overwrite all cookies. It only ...
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