admin管理员组文章数量:1245097
The question says it all, I feel like I've read everything I can and I am still no further forwards. The current situation is:
- Enter
api.mydomain
into a browser directly does save my cookie - Using Fetch from my
index.html
from myportal.mydomain
does not.
I have no CORS errors and the OPTIONS, GET and POST requests all get a 200 response. The payload in FastAPI is being correctly received as I can see the JSON data payload, just no cookie, nor can I see the cookie set in my broswer dev tools.
In my HTML file I have the following:
fetch(';count=2', {
method: 'GET',
credentials: 'include',
headers: {
"Access-Control-Allow-Origin": ";
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
const payload = {
"email": "[email protected]",
"password": "password",
"csrf": "csrf"
}
const jsonData = JSON.stringify(payload);
fetch('', {
method: 'POST',
credentials: 'include',
headers: {
"Access-Control-Allow-Origin": ";,
"Content-Type": "application/json"
},
body: jsonData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
My router looks like this for API:
@router.get("/cookie")
def set_cookie(response: Response):
# Set an HttpOnly cookie
response.set_cookie(
key="testCookie",
value="testCookieValue",
httponly=True, # This makes the cookie HttpOnly
secure=True, # Use secure cookies in production
samesite="none" # Adjust based on your needs
)
return {"message": "Cookie has been set2"}
My initial FastAPI config looks like this:
origins = [
";,
";,
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=[
"Content-Type",
"Authorization",
"X-Requested-With",
"Access-Control-Request-Method",
"Access-Control-Request-Headers",
"Access-Control-Allow-Origin"],
)
I'm not sure what else to try.
The question says it all, I feel like I've read everything I can and I am still no further forwards. The current situation is:
- Enter
api.mydomain
into a browser directly does save my cookie - Using Fetch from my
index.html
from myportal.mydomain
does not.
I have no CORS errors and the OPTIONS, GET and POST requests all get a 200 response. The payload in FastAPI is being correctly received as I can see the JSON data payload, just no cookie, nor can I see the cookie set in my broswer dev tools.
In my HTML file I have the following:
fetch('https://api.mydomain/api/v1/forms/cookie?category=all&count=2', {
method: 'GET',
credentials: 'include',
headers: {
"Access-Control-Allow-Origin": "https://portal.mydomain"
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
const payload = {
"email": "[email protected]",
"password": "password",
"csrf": "csrf"
}
const jsonData = JSON.stringify(payload);
fetch('https://api.mydomain/api/v1/forms/auth', {
method: 'POST',
credentials: 'include',
headers: {
"Access-Control-Allow-Origin": "https://portal.mydomain",
"Content-Type": "application/json"
},
body: jsonData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
My router looks like this for API:
@router.get("/cookie")
def set_cookie(response: Response):
# Set an HttpOnly cookie
response.set_cookie(
key="testCookie",
value="testCookieValue",
httponly=True, # This makes the cookie HttpOnly
secure=True, # Use secure cookies in production
samesite="none" # Adjust based on your needs
)
return {"message": "Cookie has been set2"}
My initial FastAPI config looks like this:
origins = [
"https://portal.mydomain",
"https://api.mydomain",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=[
"Content-Type",
"Authorization",
"X-Requested-With",
"Access-Control-Request-Method",
"Access-Control-Request-Headers",
"Access-Control-Allow-Origin"],
)
I'm not sure what else to try.
Share Improve this question edited Feb 15 at 15:17 Chris 34.3k10 gold badges99 silver badges234 bronze badges asked Feb 15 at 10:46 Johnny John BoyJohnny John Boy 3,2846 gold badges33 silver badges56 bronze badges 4 |1 Answer
Reset to default 4This has nothing to do with CORS. The issue is that by default, a cookie set on api.mydomain
is not available on any other subdomain, such as portal.mydomain
. To make a cookie available on all subdomains, you must explicitly set the domain to .mydomain
:
@router.get("/cookie")
def set_cookie(response: Response):
# Set an HttpOnly cookie
response.set_cookie(
key="testCookie",
value="testCookieValue",
httponly=True,
secure=True,
samesite="none",
domain=".mydomain",
)
return {"message": "Cookie has been set2"}
本文标签: corsWhy is my FastAPI endpoint not saving an HTTPonly Cookie using FetchStack Overflow
版权声明:本文标题:cors - Why is my FastAPI endpoint not saving an HTTPonly Cookie using Fetch? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740242405a2247610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
samesite
tonone
- it doesn't seem that you need it. Are you aware of the risks? – Chris Commented Feb 15 at 15:31