对简单Django视图的POST请求(使用gunicorn托管在Heroku上)导致H18服务器请求中断错误

时间:2018-11-01 00:11:20

标签: python ios django heroku

我有一个简单的Django视图,用于(非常粗略地)测试带有附加音频文件的POST请求。视图很简单:

@csrf_exempt
def create_from_audio(request):
    return HttpResponse("accepted")

但是在我的heroku日志中,我看到:

2018-10-31T23:55:02.018787+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/polls/create_from_audio" host=XXX request_id=e3608c6e-8c91-440a-ade4-b854e1f72f07 fwd="174.62.90.138" dyno=web.1 connect=0ms service=163ms status=503 bytes=199 protocol=https

以下是发送请求的代码:

let url = URL(string:"...")!
        let session = URLSession.shared
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("audio/m4a", forHTTPHeaderField: "Content-Type")
        request.httpBody = data
        let task = session.dataTask(with: request) { data, response, error in
            if let error = error {
                NSLog("send error: \(error.localizedDescription)")
                self.showLoading(message: "error sending", error: true)
            } else {
                guard let response = response as? HTTPURLResponse else { return }
                NSLog("send complete, response: \(response.statusCode)")
                if let pendingMessage = self.pendingNewMessage {
                    self.conversation.messages.append(pendingMessage)
                    self.refreshUI()
                }
            }
        }
        task.resume()

(我意识到在生产环境中直接上传到s3会更好,但就我而言,我只是试图测试使用准系统实现的文件上传的大致速度。)

1 个答案:

答案 0 :(得分:1)

我无法在Django上使用它。我切换到了Flask,即使有大约20MB的较大文件,它也可以正常工作:

@app.route('/upload', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        print("it's a post!")
        file = request.files['file']
        file.save('inprogress.m4a')
        return 'Success'