Heroku H18错误,而我的后端对路由器(Multipart/form-data)响应200



我在Heroku上部署Golang API时会出现错误。Heroku检测到断开连接并报告500错误,而在日志中,我的服务器正确回答了200。它是带有文件的multipart/form-data请求,我只需在响应主体中返回JSON。

2019-03-01T07:35:29.060814+00:00 app[web.1]: xx.x.xx.x - - [01/Mar/2019:07:35:29 +0000] "POST /v1/fixture/extract/ HTTP/1.1" 200 2133
2019-03-01T07:35:29.413179+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/v1/fixture/extract/" host=xxx.com request_id=28cdf569-2e00-47b8-9989-9280865707c8 fwd="xx.x.xx.x" dyno=web.1 connect=1ms service=352ms status=503 bytes=2280 protocol=https

这是我在没有反向代理的情况下在本地访问服务时的卷曲:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /v1/fixture/extract/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.0
> accept: application/json
> Content-Length: 154090
> Content-Type: multipart/form-data; boundary=------------------------7a9bb5de3838f429
> Expect: 100-continue
> 
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Fri, 01 Mar 2019 07:44:50 GMT
< Connection: close
< Transfer-Encoding: chunked
< 

我很难找到会发生这种情况的原因。

我的GO功能:

func FixtureExtract(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(ExtractFixture{})
}

错误的原因是我在返回答案之前没有消耗客户端发送的multipart/form-data

这解决了问题:

func FixtureExtract(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    r.ParseMultipartForm(32 << 20)
    json.NewEncoder(w).Encode(ExtractFixture{})
}

我只需要用r.ParseMultipartForm(32 << 20)

解析表格

最新更新