【问题标题】:Django view recieving incomplete POST payloadDjango 视图接收到不完整的 POST 有效负载
【发布时间】:2014-11-25 12:11:59
【问题描述】:

我有个简单的看法如下:

@csrf_exempt
def stackcommits(request):
    print request.body
    return HttpResponse("")

当我 POST 到映射到此视图的 URL 时,我的控制台显示了一个不完整且损坏的有效负载。它大约是它应有的尺寸的三分之一,即使如此,对我来说第三个也不是连续的,而是随机交付的。如下:

2014-09-30 18:47:21
<QueryDict: {u'payload': [u'{"zen":"Avoid administrative distraction.","hook_id":3103635,"hook":{"url":"https://api.github.com/repos/valeyard/SearchDemon/hooks/3103635","test_url":"https://api.github.com/repos/valeyard/SearchDemon/hooks/3103635/test","id":3103635,"name":"web","active":true,"events":["push"],"config":{"secret":"","url":"http://searchdemon.pythonanywhere.com/searchdemon/stackcommits/","content_type":"json","insecure_ssl":"0"},"last_response":{"code":null,"status":"unused","message":null},"updated_at":"2014-09-29T20:56:33Z","created_at":"2014-09-29T20:56:33Z"},"repository":{"id":24075885,"name":"SearchDemon","full_name":"valeyard/SearchDemon","owner":{"login":"valeyard","id":5278331,"avatar_url":"https://avatars.githubusercontent.com/u/5278331?v=2","gravatar_id":"","url":"https://api.github.com/users/valeyard","html_url":"https://github.com/valeyard","followers_url":"https://api.github.com/users/valeyard/followers",

2014-09-30 18:47:21
.github.com/repos/valeyard/SearchDemon/issues{/number}","pulls_url":"https://api.github.com/repos/valeyard/SearchDemon/pulls{/number}","milestones_url":"https://api.github.com/repos/valeyard/SearchDemon/milestones{/number}","notifications_url":"https://api.github.com/repos/valeyard/SearchDemon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/valeyard/SearchDemon/labels{/name}","releases_url":"https://api.github.com/repos/valeyard/SearchDemon/releases{/id}","created_at":"2014-09-15T22:10:54Z","updated_at":"2014-09-24T17:12:44Z","pushed_at":"2014-09-29T20:42:34Z","git_url":"git://github.com/valeyard/SearchDemon.git","ssh_url":"git@github.com:valeyard/SearchDemon.git","clone_url":"https://github.com/valeyard/SearchDemon.git","svn_url":"https://github.com/valeyard/SearchDemon","homepage":null,"size":1172,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":tr

有什么想法吗?有人告诉我,这不是 PA 的基础设施问题,所以很可能是我的问题。但我不知道是什么

【问题讨论】:

  • 你发帖怎么样?您能否发布一些您发布内容/方式的代码。
  • 是的,它是 Github webhook POST,我已经使用以下命令测试了 Github 是否正确发送它们:requestb.in/qhgo7nqh?inspect 你可以看到从 Github 那里发送的有效负载

标签: python django github payload pythonanywhere


【解决方案1】:

我认为您只是看到了一个日志记录工件。消息作为单个 POST 传递,但记录器正在分解消息,因为它太长了。不只是 printing 消息(将其发送到服务器日志),而是使用 print >> sys.stderr, request.body,它将出现在错误日志中而不会被截断。

【讨论】:

    【解决方案2】:

    我猜你的 print 语句正在截断隐式发生的 str(request.body) 的输出,但 POST 的实际内容在那里。试试这个:

    import pprint
    
    @csrf_exempt
    def stackcommits(request):
        pprint.pprint(request.body)
        return HttpResponse("")
    

    【讨论】:

    • 刚刚尝试过,但似乎仍然在做同样的事情。当我让 Github 使用 urlencode 标头(与 json 相对)时,我似乎在打印时始终在服务器日志中出现 3 个单独的块。我得到了开始,在中间的某个地方,然后是请求的结束。但是中间有很多随机缺失。你能想出其他方法来测试我的服务器是否正确接收数据吗?
    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2013-04-12
    • 2013-08-04
    相关资源
    最近更新 更多