Posts

Showing posts from 2012

Problem: PHP $_POST has no data

Solution: use file_get_contents("php://input"); instead. Has to do with php settings, which sometimes cause HTTP_RAW_POST_DATA to not work ! From the PHP manual : """ php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data". """

Python json : do NOT use ensure_ascii with the dump(s) method

ensure_ascii=False is evil! It does results in unicode, but with a character set that is different from 'utf8', even if you also pass encoding='utf8' . So always use the encoding parameter instead !! So, do this: json_string = json.dumps(obj = json_obj, indent = 2, encoding="utf8") Not that: json_string = json.dumps(obj = json_obj, ensure_ascii = False, indent = 2, encoding="utf8")

Handling control characters (escaping) in python for json and mysql

JSON loads() and dumps(): how control characters are handled The json lib escapes control characters when you dump(s) a python object into a json string. import json aa = "String with\ttab and\nnewline" print "1. Py string with control chars, unescaped: '%s'" % aa aa = json.dumps(aa, indent = 2, encoding="utf8") # dumping py-obj into json-string will cause control chars to be escaped print "\n2. JSON Encoded String with control chars, escaped: '%s'" % aa aa = json.loads(aa) # loading json-string into py-obj will leave control chars unescaped print "\n3. Py object (= string) with control chars, unescaped: '%s'" % aa Output: 1. Py string with control chars, unescaped: 'String with tab and newline' 2. JSON Encoded String with control chars, escaped: '"String with\ttab and\nnewline"' 3. Py object (= string) with control chars, unescaped: 'String with tab and newline' Note that json.l...

Py introspection - comments and __doc__

See http://docs.python.org/library/inspect.html def test_function(): """ Explanation of this function """ print "Testing: %s .." % test_function.__doc__ test_create_job__single_format() >> Testing: Explanation of this function ..