【问题标题】:Python Bottle API No Json Response while accessing using external API使用外部 API 访问时 Python Bottle API 无 Json 响应
【发布时间】:2013-06-28 13:02:23
【问题描述】:

我使用瓶子为 openerp 创建了一个 api,并且工作正常。

每当我使用浏览器访问时

例如:http://example.com/api/user_name=uname&password=pwd

它返回 json 值,并且在使用 python 访问相同的 api 时也会响应

但问题是当我使用 php 从外部 api 访问它时,它没有响应或返回 json 数据。

这是我的 wgsi 代码

    from bottle import Bottle,get,post,run,request,error,route,template,validate,debug,response
    import json
    import os
    import sys
    import bottle
    @route ('/user_name=:user_name&password=:password', method='GET')
    @route ('/user_name=:user_name&password=:password', method='POST')
    def login_validate(user_name,password):
        import xmlrpclib
        print "test"
        dbname = 'more'
        sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
        uid = sock_common.login(dbname, user_name, password)
        if uid:
            sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
            adv_fields = ['name']
            adv_args=[('user_id','=',uid)]
            adv_id = sock.execute(dbname, uid, password, 'res.partner', 'search', adv_args) 
            if adv_id:
                res = sock.execute(dbname, uid, password, 'res.partner', 'read',adv_id, adv_fields) [0]
                print res,type(res)
                return json.dumps({'Sucesss':res['name']})
            else:
                return json.dumps({'Error':'User Found but not a partner'})
        else:
            return json.dumps({'Failure':'Invalid User Name or Password'})
    application = bottle.default_app()

【问题讨论】:

  • 你能发布一些调试信息吗?参数是否正确发送?一些print(user_name, password) 可以提供帮助。
  • 在使用 php 内容类型访问时不返回为 json 它返回为 gzip
  • 你是如何部署这个应用程序的?涉及哪个 Web 服务器?似乎 gzip 发生在 wsgi 之后,即:nginx、apache..
  • 我正在使用 apache 服务器和 mod_wsgi
  • 那么 gzip 压缩可能在 apache 这边,你能发布配置吗?

标签: php python api rest bottle


【解决方案1】:

第一选择:禁用 Apache gzip 压缩

查看Include mods-enabled/*.loadInclude mods-enabled/*.conf,查看mods-enabled 文件夹内是否有任何对mod_deflate(Apache 的gzip 压缩)的引用。

如果您使用的是类似 Debian 的系统,您可以使用以下命令禁用它:

a2dismod deflate
service apache2 restart

如果您使用的是 RedHat/Centos,请检查 /etc/httpd/conf/httpd.conf 并编辑 LoadModule 行。

第二选择:阅读 gzip 响应

Uncompress gzip compressed http response

【讨论】:

  • 实际上问题出在 php 开发人员的 json 上,他使用 'JSON' 而不是 'JSONp'。在我检查了他的代码后,我发现问题出在他这边,请查看这个以获取详细信息stackoverflow.com/questions/2887209/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
相关资源
最近更新 更多