在 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 21332019-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< 我很難找到發生這種情況的原因。我的去功能:func FixtureExtract(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(ExtractFixture{})}
1 回答

函數式編程
TA貢獻1807條經驗 獲得超9個贊
錯誤的原因是我在返回答案之前沒有使用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)
.
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消