Django no text in response

大兔子大兔子 提交于 2020-01-07 02:56:07

问题


somewhere in my views.py,I have

def loadFcs(request):
  r = requests.get('a url')
  res = json.loads(r.text)
  #Do other stuff
  return HttpResponse('some response')

Now when I call this from my javascript, loadFcs gets called, and probably requests.get gets called asynchronously. So I end up seeing ' TypeError at /loadFcs expected string or buffer' and the trace points to the line with

res = json.loads(r.text)

I also modified my code to check whats the problem, and

def loadFcs(request):
  r = requests.get('a url')
  res = json.loads(r.text)
  if r == None:
    print 'r is none'
  if r.text == None:
    print 'text is  None'
  #Do other stuff
  return HttpResponse('some response')

and noticed that 'text is none'. So I think I need to adjust code so that request.get is synchronous. I think the method execution continues and the return statement is hit even before r.text has some value. Suggestions?


回答1:


okay, so I tried the same thing with python command line and it worked BUT not with the same code in my server. So what was the problem?

apparently, response.text is in some encoding ( UTF8) which my server was not set to receive, so it was just throwing it away and hence null.

Solution: use response.content ( which is raw binary)



来源:https://stackoverflow.com/questions/15511725/django-no-text-in-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!