用Django1.10 + Nginx + Supervisor + Gunicorn获取空的帖子请求

时间:2017-05-19 07:26:54

标签: django post nginx gunicorn supervisor

我正在使用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配置,我也有一个带有“上传按钮”的这个部分的网页版本,一切正常,nginx或supervisor的.log文件中没有错误和django安装没关系,有什么想法吗?感谢

1 个答案:

答案 0 :(得分:0)

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

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