【问题标题】:Why I'm getting two different outputs?为什么我得到两个不同的输出?
【发布时间】:2021-01-08 17:17:25
【问题描述】:

我正在尝试通过 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 格式 return 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 raise JSONDecodeError("Expecting value", s, err.value) from None

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())

【问题讨论】:

    标签: python json rest python-requests postman


    【解决方案1】:

    尝试删除引号。像这样:

    r = requests.get(url, headers=headers, params=params, verify=False)
    

    【讨论】:

    • 我的错误,在我的代码中没有引号。非常感谢
    • 除此之外,标题呢?你是在调用 requests.get 之前定义它们吗?
    • 也可能是响应根本不是JSON格式,因此JSON解码器抛出异常requests.readthedocs.io/en/master/user/quickstart/…
    • 我认为是这样的......但是为什么它在 Postman 中是 JSON 格式,而不是使用 Python requests.get 的 JSON 格式?
    • 我的猜测是 Postman 在后台解析原始响应
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 2021-06-14
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多