英文:
golang post file to flask server
问题
我试图创建一个应用程序,该应用程序将从页面上的表单发送文件到Golang服务器,然后再将其转发到Flask服务器。
我的Golang服务器代码如下:
func api_upload_model(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(50 << 20)
file, handler, err := r.FormFile("Model")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
fmt.Printf("Uploaded File: %+v\n", handler.Filename)
fmt.Printf("File Size: %+v\n", handler.Size)
resp, err := http.Post(*在此处输入URL*, "multipart/form-data", file)
if err != nil {
fmt.Println("Error Sending the File")
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "text/html")
buffer, _ := ioutil.ReadAll(resp.Body)
_, err = w.Write(buffer)
if err != nil {
fmt.Println("Error Sending the File")
fmt.Println(err)
return
}
}
func main() {
router := chi.NewRouter()
router.Post("/api/upload_model", api_upload_model)
http.ListenAndServe(":8080", router)
}
Flask服务器代码如下:
@app.route('/upload_model', methods=['POST'])
def upload_model():
log("Inference server", "123")
name = request.args.get('name', None)
if name is None:
return "Error: bad request parameters."
print(str(request))
if 'file' not in request.files:
return "No file"
file = request.files['file']
if file:
filename = secure_filename(name)
file.save(os.path.join("data/models", name))
return "200"
从Flask服务器收到的内容如下:
<Request 'http://*在此处输入URL*/upload_model?name=1.jpg' [POST]>:
我尝试打印request.files
,结果为空。所以,要么Golang服务器没有发送文件,要么Flask服务器没有接收到文件。
Golang服务器打印的结果如下:
Uploaded File: 1.jpg
File Size: 114940
因此,它成功接收到文件。
更新:
我找到了这个答案:https://stackoverflow.com/questions/66591844/how-to-redirect-multipart-post-request-to-a-second-server-in-golang
并按照其中的方法修改了我的Golang服务器代码。现在它不解析文件,而是将整个请求未经修改地发送到Flask服务器。这样更加简单。
英文:
I try to create app which will send file from the form on page to the golang server and it will resend it to flask server.
My golang server:
func api_upload_model(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(50 << 20)
file, handler, err := r.FormFile("Model")
if err != nil {
fmt.Println("Error Retrieving the File")
fmt.Println(err)
return
}
fmt.Printf("Uploaded File: %+v\n", handler.Filename)
fmt.Printf("File Size: %+v\n", handler.Size)
resp, err := http.Post(*URL here*, "multipart/form-data", file)
if err != nil {
fmt.Println("Error Sending the File")
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "text/html")
buffer, _ := ioutil.ReadAll(resp.Body)
_, err = w.Write(buffer)
if err != nil {
fmt.Println("Error Sending the File")
fmt.Println(err)
return
}
}
func main() {
router := chi.NewRouter()
router.Post("/api/upload_model", api_upload_model)
http.ListenAndServe(":8080", router)
}
Flask handler:
@app.route('/upload_model', methods=['POST'])
def upload_model():
log("Inference server", "123")
name = request.args.get('name', None)
if name is None:
return "Error: bad request parameters."
print(str(request))
if 'file' not in request.files: #I know that I don't set filename to 'file' but request.files is empty anyway
return "No file"
file = request.files['file']
if file:
filename = secure_filename(name)
file.save(os.path.join("data/models", name))
return "200"
What I get from flask server:
<Request 'http://*URL here*/upload_model?name=1.jpg' [POST]>:
I tried to print request.files: it is empty.
So, golang server doesn't send file or flask server doesn't get it.
Golang prints
Uploaded File: 1.jpg
File Size: 114940
So it recieves file.
UPD:
Found this answer: https://stackoverflow.com/questions/66591844/how-to-redirect-multipart-post-request-to-a-second-server-in-golang
and made my golang server like there. Now it doesn't parse files but sends whole request to flask server unedited. And it is more easy.
答案1
得分: 1
你不能直接将FormFile
作为输入传递给POST方法,而是需要创建一个包含文件的多部分对象。
参考示例
https://stackoverflow.com/questions/58105449/how-to-use-multipart-in-golang#58110947
英文:
You cannot directly pass the FormFile
as an input to the post method instead you need to create a multipart object that contains the file.
See example
https://stackoverflow.com/questions/58105449/how-to-use-multipart-in-golang#58110947
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论