#Decoding resty get response as image got format unknown/http2: response body closed error

21 messages · Page 1 of 1 (latest)

tame drift
#

For example, here's my code for now:

func getImage(url string) image.Image {
    client := resty.New()
    proxy := config.K.String("proxy")
    if proxy != "" {
        client.SetProxy(proxy)
        spew.Dump(proxy)
    }
    imageResp, _ := client.R().Get(url)
    data := imageResp.RawBody()
    defer data.Close()
    image, _, _ := image.Decode(data)
    return image
}

I debugged and found out that I get 200 as status code, but when reading the image I got(*errors.errorString)(0xe230b0)(image: unknown format), is there any solution? Thanks!

#

I can confirm the url I'm using returns a jpeg

strange steppe
#

Try adding import _ "image/jpeg"

tame drift
#

I got the same error

#

well, slightly different: (*errors.errorString)(0xdb2090)(image: unknown format)

#

I guess the address of errorstring doesn't matter

strange steppe
#

what if you call jpeg.Decode directly ?

tame drift
#

Let me see

#

(*errors.errorString)(0xdb1fa0)(http2: response body closed)

#

here's the code


func getImage(url string) image.Image {
    client := resty.New()
    proxy := config.K.String("proxy")
    if proxy != "" {
        client.SetProxy(proxy)
        spew.Dump(proxy)
    }
    spew.Dump(url)
    imageResp, err := client.R().Get(url)
    spew.Dump(err)
    spew.Dump(imageResp.StatusCode())
    data := imageResp.RawBody()
    defer data.Close()
    // image, _, err := image.Decode(data)
    image, err := jpeg.Decode(data)
    spew.Dump(err)
    return image
}

#

but I still got a HTTP 200

strange steppe
#

I'm starting to think you don't receive the image in the first place

tame drift
#

let me test on something else

#

here's the result testing on httpbin

#
(string) (len=30) "https://httpbin.org/image/jpeg"
(interface {}) <nil>
(int) 200
(*errors.errorString)(0xda6fa0)(http2: response body closed)
(interface {}) <nil>
#

and I turned off proxy here

#
func getImage(url string) image.Image {
    client := resty.New()
    spew.Dump(url)
    imageResp, err := client.R().Get(url)
    spew.Dump(err)
    spew.Dump(imageResp.StatusCode())
    data := imageResp.RawBody()
    defer data.Close()
    image, err := jpeg.Decode(data)
    spew.Dump(err)
    return image
}
#

not sure if it's a bug on resty as I replaced resty with http now it's working

tame drift
#

Decoding resty get response as image got format unknown error

#

Decoding resty get response as image got format unknown/http2: response body closed error