flask session doesn't persist when invoke the call from c# mvc web application

I wrote a web service in python flask. In my code I have flask session object and it looks good.

Here is a short example how my python code look like-

@app.route('/api/simpleTry', methods=['GET']) def api_simple():     user_id = request.args['id']     sentence = request.args['sentence']      if 'user_id' not in session.keys():         session['user_id'] = user_id     return jsonify("hello") app.run() 

when I invoke the service through ‘crome’ explorer it works good, the session persist and when I 2 open 2 clients (1- crome, 2-incognito) I can see that each one of them has a separate session.

in addition, I have a C# mvc web application that act as my client and over there I call the python flask service I wrote.

address = "http://localhost:5000/api/simpleTry?id=" + userId + "&sentence=" + sentence; using (var w = new WebClient()) {      json_data = w.DownloadString(address);      ... } 

The code works well. but, as opposed to ‘crome’, when sending the request from c#, the session in python does not persist and create a new session for each request.

I guess I should send something from C# to the service in order to use the existing session, but I don’t what and how.

Add Comment
1 Answer(s)

Flask sessions uses cookies to communicate session information between the client & server. When you used a browser, the browser is the client & automatically sends the cookies back to the server. When you use a C# client, I’m not sure if it does that for you automatically. You may have to read the session cookie from the header & send it along with subsequent requests.

From the Flask documentation: This is implemented on top of cookies for you and signs the cookies cryptographically

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.