当密码包含' @'时,请求无法进行身份验证

时间:2018-02-08 19:51:34

标签: python python-requests

这是一个简单的python脚本:当密码包含" @"请求失败,错误

ip = "XX.XX.XX.XX"

username='admin'

password = "kdsjkj@dfdsa"

headers = {"content-type": "application/json", "accept": "application/json"}

params = {}

params["username"] = username

params["password"] = password

baseurl = "https://ip:port/storagemgr/rest"

r = requests.post(baseurl,params=params, headers=headers, verify=False)

输出:

description:An error occurs to the parameter

suggestion:Enter a correct parameter

同样的事情在curl命令

上正常工作
curl -k -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"x@xxx"}' https://xx.xx.xx.xx:/storagemgr/rest

你能否提出任何解决方案。我尝试使用代理它不起作用。

1 个答案:

答案 0 :(得分:0)

您的python请求配置错误。您正在发送url编码的url参数而不是json正文。

查看httpbin.org // outer scope: var database = whatever; var user_uid_list = []; // inside the 1st .then call: array_subs_info.push(promise); // store the uid with the same index as the promise has user_uid_list[array_subs_info.length - 1] = user_uid; // the final .then call: }).then(function(array_subs_info) { array_subs_info.forEach(function (data, index) { // withdraw the uid with the right index var user_uid = user_uid_list[index]; }); }); 端点回应请求时的响应内容。

卷曲(正确)

post

观察响应的$ curl -k -H "Content-Type: application/json" -X POST -d '{"username":"foo","password":"b@r"}' http://httpbin.org/post { "args": {}, "data": "{\"username\":\"foo\",\"password\":\"b@r\"}", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Connection": "close", "Content-Length": "35", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "curl/7.37.0" }, "json": { "password": "b@r", "username": "foo" }, "origin": "xxx.xxx.xxx.xxx", "url": "http://httpbin.org/post" } 元素包含预期值。

请求(错误)

json

观察>>> url = 'http://httpbin.org/post' >>> headers = {"content-type": "application/json", "accept": "application/json"} >>> params = {'username': 'foo', 'password': 'b@r'} >>> r = requests.post(url, params=params, headers=headers) >>> print(r.text) { "args": { "password": "b@r", "username": "foo" }, "data": "", "files": {}, "form": {}, "headers": { "Accept": "application/json", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "0", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "python-requests/2.12.4" }, "json": null, "origin": "xxx.xxx.xxx.xxx", "url": "http://httpbin.org/post?username=foo&password=b%40r" } data元素为空,并且验证参数位于网址中。

请求(正确)

要修复请求,请在json上调用json.dumps并将其作为params参数传递给data,以便将其设置为请求正文。

requests.post