Created
December 28, 2011 17:57
-
-
Save darccio/1528886 to your computer and use it in GitHub Desktop.
Solution for question "Reading image from HTTP request's body in Go" at SO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment