Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't set cookie with flask

For development I am using vuejs which is being served by webpack at my local address: 172.18.0.77:8080 and flask that is run by Werkzeug at address 172.18.0.77:5000. I am trying to set cookie to some GET request by running this code:

response_data = Response(json.dumps(some_json_data, indent=True), status=200, mimetype='application/json')
response_data.set_cookie('user_session_id', value='12345', domain='172.18.0.77:8080')
return response_data

But when I am trying to read this cookie with following code request.cookies.get('user_session_id') I am receiving only None value.

I also tried to set cookie by changing domain to 172.18.0.77 like:

 response_data.set_cookie('user_session_id', value='12345', domain='172.18.0.77')

But it also doesn't work

like image 507
Bogdan Lashkov Avatar asked May 28 '26 13:05

Bogdan Lashkov


1 Answers

if you use axios in vuejs, i suggest that you can add withCredentials: true

const instance = axios.create({
    withCredentials: true,
    ....
})

And in flask

@app.after_request
def handle_credentials(response):
    response.headers["Access-Control-Allow-Credentials"] = True
    return response
like image 108
jiangyx3915 Avatar answered May 31 '26 06:05

jiangyx3915



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!