【问题标题】:getting empty post request with Django1.10 + Nginx + Supervisor + Gunicorn使用 Django1.10 + Nginx + Supervisor + Gunicorn 获取空帖子请求
【发布时间】:2017-10-19 05:23:44
【问题描述】:

我正在使用 curl 发出帖子请求,甚至比这更简单:

curl -u usr:pwd -H "Content-Type: multipart/form-data" -i --form "file=@/path/to/myfile/myfile.zip"  -X POST http://midominio.co/api/mypostrequest/

但我总是得到一个空的身体,这就是我得到的:

{
'session': <django.contrib.sessions.backends.db.SessionStore object at 0x7f118e502b90>, 
'_post': <QueryDict: {}>, 
'content_params': {'boundary': '------------------------axxxxxxxxx'}, 
'_post_parse_error': False, 
'_messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7f118e502d90>, 
'resolver_match': ResolverMatch(func=piston.resource.Resource, args=(), kwargs={}, url_name=None, app_names=[], namespaces=[]), 
'GET': <QueryDict: {}>, 
'_stream': <django.core.handlers.wsgi.LimitedStream object at 0x7f118e502b50>, 
'COOKIES': {}, 
'_files': <MultiValueDict: {}>, 
'_read_started': False, 
'META': {'HTTP_AUTHORIZATION': 'Basic Y2FpbjpjYWlu', 
'SERVER_SOFTWARE': 'gunicorn/19.7.1', 
'SCRIPT_NAME': u'', 
'REQUEST_METHOD': 'POST', 
'PATH_INFO': u'/api/mypostrequest/', 
'SERVER_PROTOCOL': 'HTTP/1.0', 
'QUERY_STRING': '', 
'CONTENT_LENGTH': '180', 
'HTTP_USER_AGENT': 'curl/7.51.0', 
'HTTP_CONNECTION': 'close', 
'SERVER_NAME': 'midominio.co', 
'REMOTE_ADDR': '', 
'wsgi.url_scheme': 'http', 
'SERVER_PORT': '80', 
'wsgi.input': <gunicorn.http.body.Body object at 0x7f118e5029d0>, 
'HTTP_HOST': 'midominio.co', 
'wsgi.multithread': False, 
'HTTP_ACCEPT': '*/*', 
'wsgi.version': (1, 0), 
'RAW_URI': '/api/mypostrequest/', 
'wsgi.run_once': False, 
'wsgi.errors': <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f118e502950>, 
'wsgi.multiprocess': True, 
'gunicorn.socket': <socket._socketobject object at 0x7f118e4f6de0>, 
'CONTENT_TYPE': 'multipart/form-data; boundary=------------------------axxxxxxxxx', 
'HTTP_X_FORWARDED_FOR': '186.00.00.000', 
'wsgi.file_wrapper': <class 'gunicorn.http.wsgi.FileWrapper'>}, 
'environ': {'HTTP_AUTHORIZATION': 'Basic Y2FpbjpjYWlu', 
'SERVER_SOFTWARE': 'gunicorn/19.7.1', 
'SCRIPT_NAME': u'', 
'REQUEST_METHOD': 'POST', 
'PATH_INFO': u'/api/mypostrequest/', 
'SERVER_PROTOCOL': 'HTTP/1.0', 
'QUERY_STRING': '', 
'CONTENT_LENGTH': '180', 
'HTTP_USER_AGENT': 'curl/7.51.0', 
'HTTP_CONNECTION': 'close', 
'SERVER_NAME': 'midominio.co', 
'REMOTE_ADDR': '', 
'wsgi.url_scheme': 'http', 
'SERVER_PORT': '80', 
'wsgi.input': <gunicorn.http.body.Body object at 0x7f118e5029d0>, 
'HTTP_HOST': 'midominio.co', 
'wsgi.multithread': False, 
'HTTP_ACCEPT': '*/*', 
'wsgi.version': (1, 0), 
'RAW_URI': '/api/mypostrequest/', 
'wsgi.run_once': False, 
'wsgi.errors': <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f118e502950>, 
'wsgi.multiprocess': True, 
'gunicorn.socket': <socket._socketobject object at 0x7f118e4f6de0>, 
'CONTENT_TYPE': 'multipart/form-data; boundary=------------------------axxxxxxxxx', 
'HTTP_X_FORWARDED_FOR': '186.00.00.000', 
'wsgi.file_wrapper': <class 'gunicorn.http.wsgi.FileWrapper'>}, 
'path_info': u'/api/mypostrequest/', 
'content_type': 'multipart/form-data; boundary=------------------------axxxxxxxxx', 
'path': u'/api/mypostrequest/', 
'data': <QueryDict: {}>, 
'method': 'POST', 
'user': <User: cain>

}

我使用默认的 nginx 配置,我也有这部分的 web 版本,带有“上传按钮”,一切正常,nginx 或主管的 .log 文件和 django 安装中没有错误没关系,有什么想法吗?谢谢

【问题讨论】:

  • --form "file=@/path/to/myfile/myfile.zip"
  • @e4c5 是我写这里的时候出错了,我刚刚修复了
  • 你尝试过简单的 HTML 表单吗?

标签: django nginx post gunicorn supervisord


【解决方案1】:

我试图将其视为正常请求是一个错误,这是一个 WSGIRequest,因此这意味着它需要以不同的方式处理,我使用它解决了,我希望这对其他人有所帮助:

    environ =  request.environ
    form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
    f = form['file'].file

【讨论】:

  • 以防其他人走上这条路:在我使用nginx+gunicorn+django + django-restframework + drf-writable-nested 的情况下,我相信这个答案是我的 POST multipart/form-data 请求在本地开发模式下工作的原因(没有 gunicorn 和没有 nginx)但不在我的 nginx+gunicorn 的 docker 环境中。但是:这只是请求正文中的一个区别:在 gunicorn/docker 环境中,我的字段名格式不正确,因为我使用了 npx patch 命令来处理未作为 postinstall运行的 npm 依赖项>
  • 参见stackoverflow.com/questions/47748075/… "当以 root 身份运行时,npm 不会运行任何脚本。"在我运行npm install 的Dockerfile 中,我以root 运行此命令。当我在本地开发中运行npm install 时不是这种情况,所以我的 npx 补丁是在本地开发中执行的,而不是在 Docker 映像中。
  • 补丁只是将嵌套列表字段的格式从files[0][file]='bytes...'更正为files[0]file='bytes...',后者可以由DRF MultiPartParser类解释,而前者不能,导致This field is required错误。
  • 在这里也可以看到我的回答:stackoverflow.com/a/69307185/3433137
猜你喜欢
  • 2020-12-26
  • 2013-10-05
  • 2015-04-21
  • 2014-03-31
  • 2012-11-22
  • 2014-02-19
  • 1970-01-01
  • 2019-12-05
  • 2016-04-23
相关资源
最近更新 更多