Retrieving Published Files Using Golang Gin

I have been using the Golang framework Gin for some time without problems, but now I need to process the images sent to my API.

Maybe I can figure out how to handle checking, resizing and saving the image, but for now I'm just trying to figure out how to capture the published file and assign it to a variable.

I looked at the Gin API docs but nothing jumps at me.

I am twisting my API as follows (could this be wrong?) ...

$ time curl -X POST --form upload=@ss.png -H "Content-Type: application/json" --cookie 'session=23423v243v25c08efb5805a09b5f288329003' "http://127.0.0.1:8080/v1.0/icon" --form data='{"json_name":"test json name","title":"Test","url":"http://sometest.com"}' 
+4
source share
2 answers

I will try to simply respond to getting the file name and assign it to a variable, because the processing of the json part has already been taken care of by @AlexAtNet. Let me know if this works for you.

  func (c *gin.Context) { file, header , err := c.Request.FormFile("upload") filename := header.Filename fmt.Println(header.Filename) out, err := os.Create("./tmp/"+filename+".png") if err != nil { log.Fatal(err) } defer out.Close() _, err = io.Copy(out, file) if err != nil { log.Fatal(err) } } 
+8
source

Try to get an HTTP request from gin.Context and read its Body property:

 func(c *gin.Context) { decoder := json.NewDecoder(c.Request.Body) var t struct { Name string `json:"json_name"` Title string `json:"title"` Url string `json:"url"` } err := decoder.Decode(&t) if err != nil { panic() } log.Println(t) } 
0
source

All Articles