golang post file to flask server

huangapple go评论118阅读模式
英文:

golang post file to flask server

问题

我试图创建一个应用程序,该应用程序将从页面上的表单发送文件到Golang服务器,然后再将其转发到Flask服务器。

我的Golang服务器代码如下:

  1. func api_upload_model(w http.ResponseWriter, r *http.Request) {
  2. r.ParseMultipartForm(50 << 20)
  3. file, handler, err := r.FormFile("Model")
  4. if err != nil {
  5. fmt.Println("Error Retrieving the File")
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Printf("Uploaded File: %+v\n", handler.Filename)
  10. fmt.Printf("File Size: %+v\n", handler.Size)
  11. resp, err := http.Post(*在此处输入URL*, "multipart/form-data", file)
  12. if err != nil {
  13. fmt.Println("Error Sending the File")
  14. fmt.Println(err)
  15. return
  16. }
  17. w.Header().Set("Content-Type", "text/html")
  18. buffer, _ := ioutil.ReadAll(resp.Body)
  19. _, err = w.Write(buffer)
  20. if err != nil {
  21. fmt.Println("Error Sending the File")
  22. fmt.Println(err)
  23. return
  24. }
  25. }
  26. func main() {
  27. router := chi.NewRouter()
  28. router.Post("/api/upload_model", api_upload_model)
  29. http.ListenAndServe(":8080", router)
  30. }

Flask服务器代码如下:

  1. @app.route('/upload_model', methods=['POST'])
  2. def upload_model():
  3. log("Inference server", "123")
  4. name = request.args.get('name', None)
  5. if name is None:
  6. return "Error: bad request parameters."
  7. print(str(request))
  8. if 'file' not in request.files:
  9. return "No file"
  10. file = request.files['file']
  11. if file:
  12. filename = secure_filename(name)
  13. file.save(os.path.join("data/models", name))
  14. return "200"

从Flask服务器收到的内容如下:

  1. <Request 'http://*在此处输入URL*/upload_model?name=1.jpg' [POST]>:

我尝试打印request.files,结果为空。所以,要么Golang服务器没有发送文件,要么Flask服务器没有接收到文件。

Golang服务器打印的结果如下:

  1. Uploaded File: 1.jpg
  2. 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:

  1. func api_upload_model(w http.ResponseWriter, r *http.Request) {
  2. r.ParseMultipartForm(50 &lt;&lt; 20)
  3. file, handler, err := r.FormFile(&quot;Model&quot;)
  4. if err != nil {
  5. fmt.Println(&quot;Error Retrieving the File&quot;)
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Printf(&quot;Uploaded File: %+v\n&quot;, handler.Filename)
  10. fmt.Printf(&quot;File Size: %+v\n&quot;, handler.Size)
  11. resp, err := http.Post(*URL here*, &quot;multipart/form-data&quot;, file)
  12. if err != nil {
  13. fmt.Println(&quot;Error Sending the File&quot;)
  14. fmt.Println(err)
  15. return
  16. }
  17. w.Header().Set(&quot;Content-Type&quot;, &quot;text/html&quot;)
  18. buffer, _ := ioutil.ReadAll(resp.Body)
  19. _, err = w.Write(buffer)
  20. if err != nil {
  21. fmt.Println(&quot;Error Sending the File&quot;)
  22. fmt.Println(err)
  23. return
  24. }
  25. }
  26. func main() {
  27. router := chi.NewRouter()
  28. router.Post(&quot;/api/upload_model&quot;, api_upload_model)
  29. http.ListenAndServe(&quot;:8080&quot;, router)
  30. }

Flask handler:

  1. @app.route(&#39;/upload_model&#39;, methods=[&#39;POST&#39;])
  2. def upload_model():
  3. log(&quot;Inference server&quot;, &quot;123&quot;)
  4. name = request.args.get(&#39;name&#39;, None)
  5. if name is None:
  6. return &quot;Error: bad request parameters.&quot;
  7. print(str(request))
  8. if &#39;file&#39; not in request.files: #I know that I don&#39;t set filename to &#39;file&#39; but request.files is empty anyway
  9. return &quot;No file&quot;
  10. file = request.files[&#39;file&#39;]
  11. if file:
  12. filename = secure_filename(name)
  13. file.save(os.path.join(&quot;data/models&quot;, name))
  14. return &quot;200&quot;

What I get from flask server:

  1. &lt;Request &#39;http://*URL here*/upload_model?name=1.jpg&#39; [POST]&gt;:

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

  1. Uploaded File: 1.jpg
  2. 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

huangapple
  • 本文由 发表于 2021年9月29日 17:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/69373788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定