【发布时间】:2020-04-24 23:09:38
【问题描述】:
以下是我对您的看法的代码:
import warnings
import contextlib
import json
import requests
from urllib3.exceptions import InsecureRequestWarning
old_merge_environment_settings = requests.Session.merge_environment_settings
@contextlib.contextmanager
def no_ssl_verification():
opened_adapters = set()
def merge_environment_settings(self, url, proxies, stream, verify, cert):
# Verification happens only once per connection so we need to close
# all the opened adapters once we're done. Otherwise, the effects of
# verify=False persist beyond the end of this context manager.
opened_adapters.add(self.get_adapter(url))
settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
settings['verify'] = False
return settings
requests.Session.merge_environment_settings = merge_environment_settings
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', InsecureRequestWarning)
yield
finally:
requests.Session.merge_environment_settings = old_merge_environment_settings
for adapter in opened_adapters:
try:
adapter.close()
except:
pass
with no_ssl_verification():
##350014,166545
payload = {'key1': '350014', 'key2': '166545'}
resp = requests.get('https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/params', params=payload, verify=False, headers={'Authorization': 'Token +++++private++++', 'content-type': 'application/json'})
print(resp.status_code)
print(resp.status_code)
j = resp.json()
##print(j)
jprint(resp.json())
我怎样才能做一段时间或一次发送个人身份证号码列表并将 JSON 结果发送给女巫? 我尝试粘贴一些参数,但它不起作用,产生一些错误...
我收到以下错误:
JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
如果我说:
resp = requests.get('https://rhconnect.marcopolo.com.br/api/workers/data_employee/company/1/badge/350014',
只需一个数字,它就可以工作。 这里跟随json:
200
[
{
"DT_INI_VIG_invalidez": null,
"DT_fim_VIG_invalidez": null,
"MODULO": "APOIO",
"chapa": 350014,
}
]
【问题讨论】:
-
为什么不显示完整的错误?我们无法运行代码来查看错误。而且我们无法读懂您的想法-因此您的问题毫无用处。
-
现在关于
for item in [350014,166545]: payload = {"key": item} -
如果您收到 JSON 错误,那么您应该使用
print(resp.text)来查看您得到的结果 - 可能您收到 HTML 并警告您使用了错误的命令。 -
您必须使用
/company/1/badge/" + str(params)而不是/company/1/badge/params"或/company/1/badge/{}".format(params)或最新的Pythonf-stringf"/company/1/badge/{params}"。您当前的网址发送单词"params"而不是数字 -
请参阅我的回答中的
all_results.append(resp.json())。
标签: python arrays http post get