通过Python请求的GET请求总是将响应数据作为Content-Type text / html返回

时间:2017-03-08 16:12:37

标签: python python-requests httpresponse

我正在使用Python包请求对服务器执行GET请求,该服务器在响应中返回json个数据。但是,我似乎无法将响应形成为application/json,而不是内容类型text/html

>>> headers = {'Content-type': 'application/json','Accept': 'application/json'}
>>> resp = requests.get("http://192.168.1.180/api/enodeb", headers=headers)
>>> resp.headers
{'Transfer-Encoding': 'chunked', 'Date': 'Wed, 08 Mar 2017 15:36:16 GMT', 'Content-Type': 'text/html; charset=UTF-8'}
>>> resp.text
u'<!DOCTYPE html> \n<html lang="en-GB"> \n    <head> \n        <title>Raemis Enterprise</title> \n        <link rel="stylesheet" href="/javascript/jquery.mobile-1.2.0/jquery.mobile-1.2.0.min.css" />\n        <link rel="stylesheet" href="/css/jqm-docs.css"/>\n        <link rel="stylesheet" href="/css/omc.css"/>\n        <script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>\n        <script type="text/javascript" src="/javascript/jquery.mobile-1.2.0/jquery.mobile-1.2.0.min.js"></script>\n        <script type="text/javascript" src="/javascript/jquery-validation-1.9.0/jquery.validate.min.js"></script>\n        <script type="text/javascript" src="/javascript/jquery.blockUI.min.js"></script>\n\n        <script type="text/javascript"> \n            function blockui (msg) {\n                $.blockUI({ \n                    message: \'<p><img src="/images/busy.gif" /> \' + msg + \'</p>\'\n                });\n            }\n            function unblockui () {\n                $.unblockUI();\n            }\n        </script>\n\n        \n    <script type="text/javascript"> \n        function getCookie(name) \n        {\n            var nameEQ = name + "=";\n            var ca = document.cookie.split(\';\');\n            for(var i=0;i < ca.length;i++) {\n                var c = ca[i];\n                while (c.charAt(0)==\' \') \n                    c = c.substring(1,c.length);\n                if (c.indexOf(name) == 0) \n                    return c.substring(c.indexOf(\'=\')+1, c.length);\n            }\n            return null;\n        }\n\n        function get_groupname ()\n        {\n            $.getJSON(\'/api/session?session_id=\' + getCookie(\'SESSIONID_RAEMIS\'), function (data) {\n                unblockui();\n                switch (data[0].groupname)\n                {\n                    case \'root\' :\n                    case \'admin\' :\n                    case \'sysadmin\' :\n                    case \'monitor\' :\n                        self.location = \'/admin\';\n                        break;\n                    case \'enduser\' :\n                        self.location = \'/user\';\n                        break;\n                    default :\n                       $(\'#form_login .error\').html(\'Cannot start application. Unsupported group \' + data[0].groupname);\n                       break;\n                }\n            }).error (function(xhr, status, error) {\n               var err = eval("(" + xhr.responseText + ")");\n               $(\'#form_login .error\').html(error + \':&nbsp;\' + err.error);\n               unblockui();\n            });\n        }\n\n        function on_form_submit (form) {\n            $(\'#form_login .error\').html(\'\');\n            blockui(\'Logging in...\');\n            $.post(\'/api/session\', $(form).serialize(), function (data) {\n                form.reset();\n                get_groupname();\n            }).error(function(xhr, status, error) { \n               var err = eval("(" + xhr.responseText + ")");\n               $(\'#form_login .error\').html(error + \':&nbsp;\' + err.error);\n               unblockui();\n            });\n        }\n\n        $(\'#page_login\').live(\'pageinit\', function () {\n            var form = $(\'#form_login\');\n            form.validate({\n                submitHandler: on_form_submit,\n                rules: {\n                    username: {\n                        required: true,\n                        maxlength: 30\n                    },\n                    password: {\n                        required: true,\n                        maxlength: 128\n                    },\n                }\n            });\n        });\n    </script>\n\n    </head> \n\n    <body> \n        \n    <div data-role="page" class="type-interior" data-theme="a" id="page_login">\n        <div class="simplepage">\n            <h1>Raemis Enterprise Login</h1>\n            <img style="position: absolute; top: 200px; left: 10px;" src="/images/large_brand_logo.png"/>\n            <form action="#" id="form_login">\n                <div class="error" style="height: 50px;"></div>\n                <div data-role="fieldcontain">\n                    <label for="form_login_username">Username:</label>\n                    <input type="text" name="username" id="form_login_username" />\n                </div>\n                <div data-role="fieldcontain">\n                    <label for="form_login_password">Password:</label>\n                    <input type="password" name="password" id="form_login_password" />\n                </div>\n                <div class="transparent">\n                    <button type="submit" id="submit_login">Login</a>\n                </div>\n            </form>\n        </div>\n    <div data-role="footer" class="footer-docs" data-theme="a" style="width: 100%;">\n    <p>Raemis Enterprise vRev. 21820</p>\n</div>  \n\n\n\n    </div>\n\n    </body>\n</html>\n\n\n'

使用Postman(http请求工具)执行相同的操作,返回正确的数据,而不必设置任何标题。

数据应为:

[{
"id": 1,
"enb_id": 1,
"oper_state": 2,
"admin_state": 2,
"name": "Airspan eNB",
"tac": 1,
"cell_id": 257,
"location": "site",
"mac": "",
"downlink_bandwidth": 25000,
"uplink_bandwidth": 12500,
"downlink_earfcn": 0,
"uplink_earfcn": 0,
"mimo_group": 0
}]

如何确保响应数据位于application/json Content-Type?

0 个答案:

没有答案