Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send JSON-formatted cookie data using Python on Google App Engine?

I'm trying to encode an object in a Python script and set it as a cookie so I can read it with client-side JavaScript.

I've run into problems every way I've tried to do this. Generally, the cookie is formatted in a way that makes JSON.parse() break.

My current script:

cookie = Cookie.SimpleCookie()
data = {"name": "Janet", "if_nasty": "Ms. Jackson"}
cookie['test'] = json.dumps(data)
self.response.headers.add_header("Set-Cookie", cookie.output(header=''))

... which results in

test="{\"name\": \"janet\"\054 \"if_nasty\": \"Ms. Jackson\"}"

on the client.

I don't really want to introduce a hack-y solution to replace instances of commas when they appear. Any ideas how I can pass complex data structures (both by setting and reading cookies) with Python?

like image 830
marclar Avatar asked Apr 18 '12 23:04

marclar


People also ask

How do I send a json post request in python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

How do I set json object in cookie?

We can convert a JSONObject to cookie using the toString() method and convert a cookie to JSONObject using the toJSONObject() method of org. json. Cookie class.

Does Python support json natively?

Python Supports JSON Natively! Python comes with a built-in package called json for encoding and decoding JSON data.


1 Answers

I also wanted to read a cookie (that had been set on the server) on the client. I worked around the issue by base64 encoding the JSON String, however there are a few small gotchas involved with this approach as well.

1: Base64 strings end with 0-2 equal signs, and these were being converted into the string \075. My approach is to revert those characters into equal characters on the client.

2: The base64 string is being enclosed in double quote characters in the cookie. I remove these on the client.

Server:

nav_json = json.dumps(nav_data)
nav_b64=base64.b64encode(nav_json)
self.response.set_cookie('nav_data', nav_b64)

Client:

var user_data_base64= $.cookie('nav_data');
// remove quotes from around the string
user_data_base64 = user_data_base64.replace(/"/g,"");
// replace \075 with =
user_data_base64 = user_data_base64.replace(/\\075/g,"=");
var user_data_encoded=$.base64.decode( user_data_base64 );
var user_data = $.parseJSON(user_data_encoded);

I am using 2 jquery plugins here: https://github.com/carlo/jquery-base64 and https://github.com/carhartl/jquery-cookie

Note: I consider this a hack: It would be better to re-implement the python code that encodes the cookie in javascript, however this also has the downside that you would need to notice and port and changes to that code.

I have now moved to a solution where I use a small html file to set the cookie on the client side and then redirect to the actual page requested. Here is a snippet from the JINJA2 template that I am using:

<script type="text/javascript">
var nav_data='{% autoescape false %}{{nav_data}}{% endautoescape %}';
$.cookie('nav_data', nav_data, { path: '/' });
window.location.replace("{{next}}")
</script>

Note 2: Cookies are not ideal for my use case and I will probably move on to Session or Local Storage to reduce network overhead (although my nav_data is quite small - a dozen characters or so.)

like image 80
Hamish Currie Avatar answered Nov 14 '22 23:11

Hamish Currie