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

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

Read request payload in Go?

问题

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

func Upload(w http.ResponseWriter, r *http.Request) {
    reader, err := r.MultipartReader()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    // 将每个部分复制到目标位置。
    for {
        part, err := reader.NextPart()
        if err == io.EOF {
            break
        }
        if part.FormName() == "avatar_data" {
            // 如何读取“avatar_data”中的内容?
        }
        if part.FileName() == "" {
            continue
        }
        dst, err := os.Create("/Users/macadmin/test/" + part.FileName())
        defer dst.Close()
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        if _, err := io.Copy(dst, part); err != nil {

            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
    }
    img, _ := imaging.Open("/Users/macadmin/test/cry3.jpg")
    if err != nil {
        panic(err)
    }

    rect := image.Rect(0, 0, 200, 500)
    // rect := image.Rectangle{20,20}
    dst := imaging.Crop(img, rect)
    err = imaging.Save(dst, "/Users/macadmin/test/cry4.jpg")
    if err != nil {
        panic(err)
    }
    // 显示成功消息。
}

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

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

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

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

英文:

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

func Upload(w http.ResponseWriter, r *http.Request) {
	reader, err := r.MultipartReader()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	//copy each part to destination.
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		if part.FormName() == &quot;avatar_data&quot;{
           // Read the content in &quot;avatar_data&quot; how?
		}
		if part.FileName() == &quot;&quot; {
			continue
		}
		dst, err := os.Create(&quot;/Users/macadmin/test/&quot; + part.FileName())
		defer dst.Close()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if _, err := io.Copy(dst, part); err != nil {

			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	img, _ := imaging.Open(&quot;/Users/macadmin/test/cry3.jpg&quot;)
	if err != nil {
		panic(err)
	}

	rect := image.Rect(0, 0, 200, 500)
	// rect := image.Rectangle{20,20}
	dst := imaging.Crop(img, rect)
	err = imaging.Save(dst, &quot;/Users/macadmin/test/cry4.jpg&quot;)
	if err != nil {
		panic(err)
	}
	//display success message.
}

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",则执行以下操作:

j, err := ioutil.ReadAll(part)
if err != nil {
    //处理错误
}
//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.

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

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:

确定