Go
- 9 ответов
- 0 вопросов
2
Вклад в тег
func postImg(url string, img io.Reader) (server int, photo, hash string, err error) {
type UploadResponse struct {
Server int `json:"server"`
Photo string `json:"photo"`
Hash string `json:"hash"`
}
var b bytes.Buffer
w := multipart.NewWriter(&b)
fw, err := w.CreateFormFile("photo", "photo.jpg")
if err != nil {
return
}
if _, err = io.Copy(fw, img); err != nil {
return
}
w.Close()
req, err := http.NewRequest("POST", url, &b)
if err != nil {
return
}
req.Header.Set("Content-Type", w.FormDataContentType())
// Submit the request
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return
}
// resp
uplRes := UploadResponse{}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&uplRes)
if err != nil {
return
}
defer res.Body.Close()
server = uplRes.Server
photo = uplRes.Photo
hash = uplRes.Hash
return
}