【问题标题】:python requests library .json() + django 500python 请求库 .json() + django 500
【发布时间】:2020-03-11 23:43:17
【问题描述】:

当我将 requests 库与 django 一起使用时,我得到一个 500 错误。 response.json() 给了我这个错误:

  response = requests.post(....)
            print("-----------------------block found!!!-----------------------")
            print(response.status_code)
            print(response.json())

json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)

有没有办法用请求库以可读的方式表示 django 500 响应?

【问题讨论】:

    标签: python django python-requests


    【解决方案1】:

    假设您收到 HTTP 500 响应,您肯定会收到一个空源,这会阻止 .json() 函数发生。

    为什么不写一个异常子句来处理异常,如下所示:


    try:
        response = requests.post(....)
        print("-----------------------block found!!!-----------------------")
        print(response.status_code)
        print(response.json())
    except HTTPError as e:
        print('Error has occurred: ', e.response.status_code)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      相关资源
      最近更新 更多