Change API response header for JSON content type and the return of JSONP data.

The API should return application/json Content-Type for JSON data instead of application/html.
This commit is contained in:
JackDandy 2016-01-12 02:07:41 +00:00
parent 53e4a0ecaa
commit ec6190d98c
2 changed files with 6 additions and 5 deletions

View file

@ -4,6 +4,7 @@
* Update xmltodict library 0.9.2 (579a005) to 0.9.2 (eac0031) * Update xmltodict library 0.9.2 (579a005) to 0.9.2 (eac0031)
* Update Tornado Web Server 4.3.dev1 (1b6157d) to 4.4.dev1 (c2b4d05) * Update Tornado Web Server 4.3.dev1 (1b6157d) to 4.4.dev1 (c2b4d05)
* Update change to suppress reporting of Tornado exception error 1 to updated package (ref:hacks.txt) * Update change to suppress reporting of Tornado exception error 1 to updated package (ref:hacks.txt)
* Change API response header for JSON content type and the return of JSONP data
### 0.11.0 (2016-01-10 22:30:00 UTC) ### 0.11.0 (2016-01-10 22:30:00 UTC)

View file

@ -135,19 +135,19 @@ class Api(webserve.BaseHandler):
self.finish(outputCallback(outDict)) self.finish(outputCallback(outDict))
def _out_as_json(self, dict): def _out_as_json(self, dict):
self.set_header('Content-Type', 'application/json') self.set_header('Content-Type', 'application/json; charset=UTF-8')
try: try:
out = json.dumps(dict, indent=self.intent, sort_keys=True) out = json.dumps(dict, indent=self.intent, sort_keys=True)
if 'jsonp' in self.request.query_arguments: callback = self.get_query_argument('callback', None) or self.get_query_argument('jsonp', None)
out = self.request.arguments['jsonp'] + '(' + out + ');' # wrap with JSONP call if requested if None is not callback:
out = '%s(%s);' % (callback, out) # wrap with JSONP call if requested
except Exception as e: # if we fail to generate the output fake an error except Exception as e: # if we fail to generate the output fake an error
logger.log(u'API :: ' + traceback.format_exc(), logger.DEBUG) logger.log(u'API :: ' + traceback.format_exc(), logger.DEBUG)
out = '{"result":"' + result_type_map[RESULT_ERROR] + '", "message": "error while composing output: "' + ex( out = '{"result":"' + result_type_map[RESULT_ERROR] + '", "message": "error while composing output: "' + ex(
e) + '"}' e) + '"}'
tornado_write_hack_dict = {'unwrap_json': out} return out
return tornado_write_hack_dict
def _grand_access(self, realKey, apiKey, args, kwargs): def _grand_access(self, realKey, apiKey, args, kwargs):
""" validate api key and log result """ """ validate api key and log result """