I'm currently developing my first webapp, frontend with React
and backend with FastAPI
.
I'm trying to test it out jointly with Chrome-- see if the frontend makes the correct API calls to backend, and display the results. I've been having problems with cookies, and I'd like help. Apologies in advance for the long post – I've been going through many resources past couple of days, and at this point I'm unsure what's relevant and what's not.
- Frontend on
localhost:8080
- Backend on
http://127.0.0.1:8000
- Proper settings for CORS (I believe) with the following
FastAPI
backend code:
app = FastAPI()
origins = [
"http://localhost:8080"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Situation: Frontend makes a GET
request to http://127.0.0.1:8000/api/abc
on backend, the backend sets a cookie.
/*====================
Attempt 1:
set cookie with the following backend code:
response.set_cookie(key="abcCookieKey", value="abcCookieValue")
and make the GET
request with the following frontend JS code:
fetch('http://127.0.0.1:8000/api/abc', {
method: 'GET',
credentials: 'include',
})
Result with attempt 1:
on the Console
tab of Chrome, I get the following warning:
A cookie associated with a cross-site resource at http://127.0.0.1/ was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
and on the network tab I get the following message when examining the set-cookie
response header:
This Set-Cookie was blocked because it has the "SameSite=Lax" attribute but came from a cross-site response which was not the response to a top-level navigation.
====================*/
...so I do some research, and come up with
/*====================
Attempt 2:
set cookie with the following backend code:
response.set_cookie(key="abcCookieKey", value="abcCookieValue", samesite="none", secure=True)
and make the GET
request with the same frontend JS code.
Result with attempt 2:
on the Console
tab of Chrome, I get the exact same warning as from attempt 1, even though the response header has a set-cookie
with Samesite=none; Secure
. Additionally, the header has the following warning
This Set-Cookie was blocked because it had the "Secure" attribute but was not received over a secure connection.
====================*/
..so I try to use https
and come up with:
/*====================
Attempt 3:
Everything the same as attempt #2, except in my JS fetch code, I use fetch('https://127.0.0.1:8000/api/abc ...
and then I get the following warning on my backend running with uvicorn
: WARNING: Invalid HTTP request received.
====================*/
Questions:
- am I missing something with attempt #2? surely there must be an easy way to set a cookie from backend to frontend without worrying about https?
- if I don't have a choice but to use
https
, how do I locally run a backend server that can be accessed withhttps
? The research I did made it seem like it was a complicated/time-consuming process. (But, to be fair, my understanding of web-dev/all things network is very limited).