I'm setting up an image encoder function where you input an image URL and it returns the ISO-8859-1 version of it. How would I write a function that sends an HTTP GET request to the URL and converts those bytes to ISO-8859-1? The code below is everything I have so far.
func grabImageBytes(imageURL string) ([]byte, error) {
req, _ := http.NewRequest("GET", imageURL, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
} else {
return body, nil
}
}
other func:
func getRandomImage(keyword string) (string, error) {
req, _ := http.NewRequest("GET", "https://www.google.com/search?tbm=isch&q="+keyword, nil)
req.Header.Add("authority", "www.google.com")
req.Header.Add("upgrade-insecure-requests", "1")
req.Header.Add("referer", "https://images.google.com/")
req.Header.Add("accept-language", "en-US,en;q=0.9")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var imageURL string
if strings.Contains(string(body), ",\"ou\":\"") {
imageURL = strings.Split(strings.Split(string(body), ",\"ou\":\"")[1], "\",\"ow\":")[0]
} else {
return "", errors.New("Image not found.")
}
req2, _ := http.NewRequest("GET", imageURL, nil)
res2, _ := http.DefaultClient.Do(req2)
defer res2.Body.Close()
if res2.StatusCode == 404 {
return "", errors.New("Image not found.")
} else {
return imageURL, nil
}
}
Your claim that the Spotify profile picture image is ISO 8850-1 encoded/encrypted makes no sense.
What would make more sense is that it is Base64 encoded.
For example,
Spotify for Developers: Web API: Upload a Custom Playlist Cover Image.
Base64 encoded JPEG image data, maximum payload size is 256 KB
In Go,
import "encoding/base64"
Package base64 implements base64 encoding as specified by RFC 4648.
Another piece of evidence: "HTTPS requests in UTF-8 format"
Spotify for Developers: Web API
Requests
The Spotify Web API is based on REST principles. Data resources are accessed via standard HTTPS requests in UTF-8 format to an API endpoint.
For example. using your Stack Overflow profile picture:
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func grabImageBytes(imageURL string) ([]byte, error) {
req, err := http.NewRequest("GET", imageURL, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
enc := base64.StdEncoding
img := make([]byte, enc.EncodedLen(len(body)))
enc.Encode(img, body)
return img, nil
}
func main() {
imageURL := `https://lh5.googleusercontent.com/-P8ICR-LXoBs/AAAAAAAAAAI/AAAAAAAAE04/fVAeB6_nMeg/photo.jpg?sz=328`
img, err := grabImageBytes(imageURL)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(string(img))
}