CherryPy JSON与Form POST [英] CherryPy JSON vs Form POST
问题描述
我有一个使用CherryPy框架用Python开发的Web应用程序.它使用许多常规的HTML表单(带有x-www-form-urlencoded数据).
I have a web application developed in Python with the CherryPy framework. It uses many conventional HTML forms (with x-www-form-urlencoded data).
我正在开发一个iOS应用程序,该应用程序想与该Web应用程序使用的数据库同步.要从iOS上传到数据库,我想使用HTTP POST方法在HTTP请求正文中上传JSON.
I'm developing an iOS application that I want to sync with the same database that the web application uses. To upload from iOS to the database I want to upload JSON in the HTTP request body with the HTTP POST method.
我尝试在Web应用程序中编写一种方法来处理上传的JSON数据.
I tried writing a method in my web application to process the uploaded JSON data.
如果我的cherrypy.config看起来像这样
If my cherrypy.config looks like this
cherrypy.config.update({
'environment': 'production',
'log.screen': False,
'log.access_file': '/home/adamek/webapps/cocoa_foody/logs/access.log',
'log.error_file': '/home/adamek/webapps/cocoa_foody/logs/error.log',
'server.socket_host': '127.0.0.1',
'server.socket_port': 15982,
'tools.caching.on': False,
'tools.encode.encoding': "utf-8",
'tools.json_in.on': True,
})
我的JSON页面可以工作,但是其他Web应用程序中的表单提交失败.
my JSON pages work, but form submissions from the rest of my web application fail.
如果我将最后一行更改为'tools.json_in.on':False,
我的旧表单页面再次工作,但是JSON POST页面失败(cherrypy.request.json为None).
If I change the last line to 'tools.json_in.on': False,
My old form pages work again, but the JSON POST pages fail (cherrypy.request.json is None).
对JSON页面的请求在请求标头中具有 Content-Type:application/json
.
The requests to the JSON pages have Content-Type: application/json
in the request headers.
对其他表单页面的请求具有内容类型:application/x-www-form-urlencoded'
The request to the other form pages have 'Content-Type: application/x-www-form-urlencoded '
有没有办法让一个CherryPy应用程序同时处理application/json和application/x-www-form-urlencoded,还是我需要拥有两个单独的CherryPy应用程序?
Is there any way to have one CherryPy application handle both application/json and application/x-www-form-urlencoded, or do I need to have two separate CherryPy applications?
推荐答案
找到了!只需在配置中添加'tools.json_in.force':False
.
Found it! Just needed to add 'tools.json_in.force': False,
to the config.
cherrypy.config.update({
'environment': 'production',
'log.screen': False,
'log.access_file': '/home/adamek/webapps/cocoa_foody/logs/access.log',
'log.error_file': '/home/adamek/webapps/cocoa_foody/logs/error.log',
'server.socket_host': '127.0.0.1',
'server.socket_port': 15982,
'tools.caching.on': False,
'tools.encode.encoding': "utf-8",
'tools.json_in.on': True,
'tools.json_in.force': False,
})
这篇关于CherryPy JSON与Form POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!