go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.68k stars 681 forks source link

can't use image.decode on rawbody #784

Closed glyh closed 4 months ago

glyh commented 4 months ago

Here's an example code:

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
}

I got 200 as return code with an jpeg url, but decoding throws error: (*errors.errorString)(0xda6fa0)(http2: response body closed)

With go's builtin http module, it works, however

imageResp, _ := client.Get(imageUrl)
data := imageResp.Body
defer data.Close()
image,  _ := jpeg.Decode(data)

Related: I posted this originally on discord https://discord.com/channels/118456055842734083/1219196701886119946/1219196701886119946

glyh commented 4 months ago

https://github.com/go-resty/resty/issues/369 use byte reader instead if not using that option.