在Go语言中读取请求的有效载荷可以通过以下方式实现:

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

Read request payload in Go?

问题

我正在使用一个文件上传器,并需要从请求负载中获取详细信息来进行裁剪。

  1. func Upload(w http.ResponseWriter, r *http.Request) {
  2. reader, err := r.MultipartReader()
  3. if err != nil {
  4. http.Error(w, err.Error(), http.StatusInternalServerError)
  5. return
  6. }
  7. // 将每个部分复制到目标位置。
  8. for {
  9. part, err := reader.NextPart()
  10. if err == io.EOF {
  11. break
  12. }
  13. if part.FormName() == "avatar_data" {
  14. // 如何读取“avatar_data”中的内容?
  15. }
  16. if part.FileName() == "" {
  17. continue
  18. }
  19. dst, err := os.Create("/Users/macadmin/test/" + part.FileName())
  20. defer dst.Close()
  21. if err != nil {
  22. http.Error(w, err.Error(), http.StatusInternalServerError)
  23. return
  24. }
  25. if _, err := io.Copy(dst, part); err != nil {
  26. http.Error(w, err.Error(), http.StatusInternalServerError)
  27. return
  28. }
  29. }
  30. img, _ := imaging.Open("/Users/macadmin/test/cry3.jpg")
  31. if err != nil {
  32. panic(err)
  33. }
  34. rect := image.Rect(0, 0, 200, 500)
  35. // rect := image.Rectangle{20,20}
  36. dst := imaging.Crop(img, rect)
  37. err = imaging.Save(dst, "/Users/macadmin/test/cry4.jpg")
  38. if err != nil {
  39. panic(err)
  40. }
  41. // 显示成功消息。
  42. }

我没有10个声望来发布POST请求的图像,但它包含以下内容:

  1. <pre>
  2. Content-Disposition: form-data; name="avatar_data"
  3. {"x":528,"y":108,"height":864,"width":864}
  4. </pre>

所以我需要从avatar_data中获取xyheightwidth。我知道我需要将JSON进行编组,但我不确定如何达到这一点。

英文:

I'm using a file uploader and need details from the request payload to crop it.

  1. func Upload(w http.ResponseWriter, r *http.Request) {
  2. reader, err := r.MultipartReader()
  3. if err != nil {
  4. http.Error(w, err.Error(), http.StatusInternalServerError)
  5. return
  6. }
  7. //copy each part to destination.
  8. for {
  9. part, err := reader.NextPart()
  10. if err == io.EOF {
  11. break
  12. }
  13. if part.FormName() == &quot;avatar_data&quot;{
  14. // Read the content in &quot;avatar_data&quot; how?
  15. }
  16. if part.FileName() == &quot;&quot; {
  17. continue
  18. }
  19. dst, err := os.Create(&quot;/Users/macadmin/test/&quot; + part.FileName())
  20. defer dst.Close()
  21. if err != nil {
  22. http.Error(w, err.Error(), http.StatusInternalServerError)
  23. return
  24. }
  25. if _, err := io.Copy(dst, part); err != nil {
  26. http.Error(w, err.Error(), http.StatusInternalServerError)
  27. return
  28. }
  29. }
  30. img, _ := imaging.Open(&quot;/Users/macadmin/test/cry3.jpg&quot;)
  31. if err != nil {
  32. panic(err)
  33. }
  34. rect := image.Rect(0, 0, 200, 500)
  35. // rect := image.Rectangle{20,20}
  36. dst := imaging.Crop(img, rect)
  37. err = imaging.Save(dst, &quot;/Users/macadmin/test/cry4.jpg&quot;)
  38. if err != nil {
  39. panic(err)
  40. }
  41. //display success message.
  42. }

I don't have 10 rep to post the image of the POST request, but it has

<pre>
Content-Disposition: form-data; name="avatar_data"

{"x":528,"y":108,"height":864,"width":864}
</pre>

So from avatar_data I need the x, y, height, and width. I know I'll have to marshal the JSON but I'm not sure how to get to that point?

答案1

得分: 4

multipart.Part 实现了 io.Reader 接口。

如果 part.FormName() 等于 "avatar_data",则执行以下操作:

  1. j, err := ioutil.ReadAll(part)
  2. if err != nil {
  3. //处理错误
  4. }
  5. //j == []byte(`{&quot;x&quot;:528,&quot;y&quot;:108,&quot;height&quot;:864,&quot;width&quot;:864}`),对其进行处理。
英文:

multipart.Part implements the io.Reader interface.

  1. if part.FormName() == &quot;avatar_data&quot; {
  2. j, err := ioutil.ReadAll(part)
  3. if err != nil {
  4. //do something
  5. }
  6. //j == []byte(`{&quot;x&quot;:528,&quot;y&quot;:108,&quot;height&quot;:864,&quot;width&quot;:864}`), do something with it.
  7. }

huangapple
  • 本文由 发表于 2014年7月31日 22:39:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/25061784.html
匿名

发表评论

匿名网友

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

确定