为什么我得到两个不同的输出?

时间:2020-09-22 14:40:54

标签: python json rest python-requests postman

我正在尝试通过REST访问IBM Maximo。 通过执行GET请求来检查特定事件是否存在,我可以使用Postman做到这一点: Postman image。 现在,我想在Python中执行此操作,但无法获得相同的输出:

params = {
    'oslc.select':'*', 
    'oslc.where':'ticketid="IN43550232"',
    }

r = requests.get(url,params=params)

当我 print(r.text)时,输出是页面的html,而当我 print(r.json())时,我得到:

文件“ C:\ Users \ User.spyder-py3 \ Projects \ GET.py”,行 27,在 打印(r.json())

文件 “ C:\ Users \ User \ anaconda3 \ lib \ site-packages \ requests \ models.py”, 第897行,在json中 返回complexjson.loads(self.text,** kwargs)

文件“ C:\ Users \ User \ anaconda3 \ lib \ json_ init _。py”, 负载中的第348行 返回_default_decoder.decode(s)

文件“ C:\ Users \ User \ anaconda3 \ lib \ json \ decoder.py”,行 解码中的337 obj,end = self.raw_decode(s,idx = _w(s,0).end())

文件“ C:\ Users \ User \ anaconda3 \ lib \ json \ decoder.py”,行 355,在raw_decode中 从None提高JSONDecodeError(“期望值”,s,err.value)

JSONDecodeError:期望值

据我所知,输出不是json格式...我的问题是,我该如何解决这个问题?谢谢

解决方案:

headers = {
    'Cookie':'...',
    'Accept':'*/*',
    'Accept-Encoding':'gzip, deflate, br',
    'Connection':'keep-alive',
    }
params = {
    'oslc.select':'*', 
    'oslc.where':'ticketid="IN43550232"',
    }

r = requests.get(url, headers=headers, params=params)
r.encoding = 'utf-8'
print(r.json())

1 个答案:

答案 0 :(得分:1)

尝试删除引号。像这样:

items
相关问题