public
Created

Solution for question "Reading image from HTTP request's body in Go" at SO

  • Download Gist
so-cutter.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
package main
 
import (
"fmt"
"http"
"io"
)
 
var client = http.Client{}
 
func cutterHandler(res http.ResponseWriter, req *http.Request) {
reqImg, err := client.Get("http://www.google.com/intl/en_com/images/srpr/logo3w.png")
if err != nil {
fmt.Fprintf(res, "Error %d", err)
return
}
buffer := make([]byte, reqImg.ContentLength)
io.ReadFull(reqImg.Body, buffer)
res.Header().Set("Content-Length", fmt.Sprint(reqImg.ContentLength))
res.Header().Set("Content-Type", reqImg.Header.Get("Content-Type"))
res.Write(buffer)
req.Body.Close()
}
 
func main() {
http.HandleFunc("/cut", cutterHandler)
http.ListenAndServe(":8080", nil) /* TODO Configurable */
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.