python - 获取 json 请求的 Django 问题

标签 python django json

我有一个前端服务器从我的后端服务器获取一些 JSON 数据。两台服务器都运行 Django。这是获取 json 数据的确切代码..

def View(request):
  r = requests.get(path)
  return HttpResponse(r.json())

但是,我今天遇到了一个奇怪的问题,即在重新启动服务器后调用成功完成一次。如果我运行以下代码:-

def View(request):
  r = requests.get(path)
  r = requests.get(path)
  return HttpResponse(r.json())

这也很成功。

但是,在第二次调用 View() 时,出现错误。错误消息是这样说的:

"uWSGI exceptions catcher for "GET /api/v1/backend/" (request plugin: "python", modifier1: 0)
Exception: TypeError: http header value must be a string
Exception class: TypeError
Exception message: http header value must be a string"

很明显,错误是在我的后端服务器上引发的,但我今天只更改了前端的一些模板。我不知道是什么导致这个问题今天开始出现。谁能指出我正确的方向?

最佳答案

给它正确的 json 头:

return HttpResponse(data, content_type='application/json')

关于python - 获取 json 请求的 Django 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200372/

相关文章:

javascript - Rails + Ember + Ember 数据空 JSON 响应

python - Django "for"循环和python字典问题

django - 如何在 Django 抽象模型类中动态命名权限?

python - Django Rest API POST 问题

python - 使用 eyeD3 从 Python 中的 mp3 文件中检索歌词

javascript - 在 json 中使用游标时无法读取未定义的属性 'length'

javascript - 如何格式化这个json

python - 为什么在这里引发 IndentationError 而不是 SyntaxError?

python - Python Tkinter 中的部分边框

python pytest 偶尔会因 OSError : reading from stdin while output is captured 而失败