I'm getting empty file when I try to read the request body of my HTTP request using Go Gin. On my request I'm sending a file attached to the Request Body (Content Type: application/octet-stream).
Request Headers
Authorization: Bearer XXX
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Postman-Token: XXX
Host: localhost:8080
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 12
Content-Type: application/octet-stream
Request Body
src: "Path to my file"
Backend Go code:
bodySize, _ = strconv.ParseInt(c.Request.Header.Get("Content-Length"), 10, 64)
buf := bytes.NewBuffer(make([]byte, 0, bodySize))
_, err = io.Copy(buf, c.Request.Body)
fmt.Println(buf.String())
The print sentence prints empty string but the file is not empty (Content Length is 12).
bodySize
and request's body to ensure it has some value?buf
value is:[]uint8 len: 0, cap: 12, []
when I initialize it and after the copy changes to:[]uint8 len: 0, cap: 536, []
but still empty...