request icon indicating copy to clipboard operation
request copied to clipboard

encode error

Open prettyyjnic opened this issue 7 years ago • 5 comments

if i use your library i got the uncorrect encode return , but i got the correct return while i use the http library

testResp, err := http.Get("http://192.168.142.128/test.php") if err != nil { log.Println(err.Error) return } log.Println("test") bt, _ := ioutil.ReadAll(testResp.Body) log.Println(string(bt)) // output 1

c := new(http.Client)
req := request.NewRequest(c)

resp, err := req.Get("http://192.168.142.128/test.php")
if err != nil {
    log.Fatalln(err.Error())
} else {
    bt, _ = ioutil.ReadAll(resp.Body)
    log.Println(string(bt)) // out put uncorrect string
}
return

prettyyjnic avatar Aug 25 '16 08:08 prettyyjnic

@prettyyjnic Thanks for your report. But I can't reproduce it. My test code:

test.go

package main

import (
        "fmt"
        "io/ioutil"
        "net/http"
        "reflect"

        "github.com/mozillazg/request"
)

func test1(url string) (bt []byte, err error) {
        testResp, err := http.Get(url)
        if err != nil {
            return
        }
        bt, err = ioutil.ReadAll(testResp.Body)
        return
}

func test2(url string) (bt []byte, err error) {
        c := new(http.Client)
        req := request.NewRequest(c)
        resp, err := req.Get(url)
        if err != nil {
            return
        }
        bt, err = ioutil.ReadAll(resp.Body)
        return
}

func main() {
        url := "https://httpbin.org/status/418"
        b1, _ := test1(url)
        b2, _ := test2(url)
        fmt.Println(b1)
        fmt.Println(b2)
        fmt.Println(reflect.DeepEqual(b1, b2))
}

result

$ go run test.go
[10 32 32 32 32 45 61 91 32 116 101 97 112 111 116 32 93 61 45 10 10 32 32 32 32 32 32 32 95 46 46 46 46 95 10 32 32 32 32 32 46 39 32 32 95 32 95 32 96 46 10 32 32 32 32 124 32 46 34 96 32 94 32 96 34 46 32 95 44 10 32 32 32 32 92 95 59 96 34 45 45 45 34 96 124 47 47 10 32 32 32 32 32 32 124 32 32 32 32 32 32 32 59 47 10 32 32 32 32 32 32 92 95 32 32 32 32 32 95 47 10 32 32 32 32 32 32 32 32 96 34 34 34 96 10]
[10 32 32 32 32 45 61 91 32 116 101 97 112 111 116 32 93 61 45 10 10 32 32 32 32 32 32 32 95 46 46 46 46 95 10 32 32 32 32 32 46 39 32 32 95 32 95 32 96 46 10 32 32 32 32 124 32 46 34 96 32 94 32 96 34 46 32 95 44 10 32 32 32 32 92 95 59 96 34 45 45 45 34 96 124 47 47 10 32 32 32 32 32 32 124 32 32 32 32 32 32 32 59 47 10 32 32 32 32 32 32 92 95 32 32 32 32 32 95 47 10 32 32 32 32 32 32 32 32 96 34 34 34 96 10]
true

What is the "uncorrect string" ? Can you post it here?

mozillazg avatar Aug 25 '16 13:08 mozillazg

@mozillazg I found the reason . If the website compress with the gzip , i would got the uncorrect return. When i decode it by myself, i got the correct return

 package

 main

import (
    "compress/gzip"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "reflect"

    "github.com/mozillazg/request"
)

func test1(url string) (bt []byte, err error) {
    testResp, err := http.Get(url)
    if err != nil {
        return
    }
    bt, err = ioutil.ReadAll(testResp.Body)
    return
}

func test2(url string) (bt []byte, err error) {
    c := new(http.Client)
    req := request.NewRequest(c)
    resp, err := req.Get(url)
    if err != nil {
        return
    }
    bt, err = ioutil.ReadAll(resp.Body)
    return
}

func test3(url string) (bt []byte, err error) {
    c := new(http.Client)
    req := request.NewRequest(c)
    resp, err := req.Get(url)
    if err != nil {
        return
    }
    var reader io.ReadCloser
    if resp.Header.Get("Content-Encoding") == "gzip" {
        reader, err = gzip.NewReader(resp.Body)
        if err != nil {
            return
        }
    } else {
        reader = resp.Body
    }

    bt, err = ioutil.ReadAll(reader)
    return
}

func main() {
    urls := [...]string{"https://www.baidu.com", "https://httpbin.org/status/418"}
    for i := range urls {
        url := urls[i]
        fmt.Println("\n----------\n")
        fmt.Println("url =", url)

        b1, _ := test1(url)
        b2, _ := test2(url)
        fmt.Println(b1)
        fmt.Println(b2)
        fmt.Println(reflect.DeepEqual(b1, b2))

        fmt.Println("\n----------\n")

        b3, _ := test3(url)
        fmt.Println(b3)
        fmt.Println(reflect.DeepEqual(b1, b3))
    }

}

the result is :

----------

url = https://www.baidu.com
[60 104 116 109 108 62 13 10 60 104 101 97 100 62 13 10 9 60 115 99 114 105 112 116 62 13 10 9 9 108 111 99 97 116 105 111 110 46 114 101 112 108 97 99 101 40 108 111 99 97 116 105 111 110 46 104 114 101 102 46 114 101 112 108 97 99 101 40 34 104 116 116 112 115 58 47 47 34 44 34 104 116 116 112 58 47 47 34 41 41 59 13 10 9 60 47 115 99 114 105 112 116 62 13 10 60 47 104 101 97 100 62 13 10 60 98 111 100 121 62 13 10 9 60 110 111 115 99 114 105 112 116 62 60 109 101 116 97 32 104 116 116 112 45 101 113 117 105 118 61 34 114 101 102 114 101 115 104 34 32 99 111 110 116 101 110 116 61 34 48 59 117 114 108 61 104 116 116 112 58 47 47 119 119 119 46 98 97 105 100 117 46 99 111 109 47 34 62 60 47 110 111 115 99 114 105 112 116 62 13 10 60 47 98 111 100 121 62 13 10 60 47 104 116 109 108 62]
[31 139 8 0 0 0 0 0 0 3 60 143 209 10 131 48 12 69 159 29 236 31 164 79 19 182 102 207 51 250 47 181 205 104 161 182 174 198 201 254 126 173 72 159 114 73 194 57 92 180 60 251 241 122 65 75 202 228 217 224 170 147 91 184 196 198 71 173 216 197 32 19 45 94 105 186 213 133 77 244 174 91 97 153 151 245 5 32 238 71 44 169 235 250 2 131 74 67 56 13 56 69 243 59 76 33 158 87 156 137 85 91 40 15 250 108 238 59 136 140 79 180 90 209 234 24 152 2 15 226 217 111 201 15 229 39 227 247 125 151 147 114 102 147 58 206 32 70 132 202 202 85 224 52 100 101 41 247 7 0 0 255 255 3 0 8 238 57 131 227 0 0 0]
false

----------

[60 104 116 109 108 62 13 10 60 104 101 97 100 62 13 10 9 60 115 99 114 105 112 116 62 13 10 9 9 108 111 99 97 116 105 111 110 46 114 101 112 108 97 99 101 40 108 111 99 97 116 105 111 110 46 104 114 101 102 46 114 101 112 108 97 99 101 40 34 104 116 116 112 115 58 47 47 34 44 34 104 116 116 112 58 47 47 34 41 41 59 13 10 9 60 47 115 99 114 105 112 116 62 13 10 60 47 104 101 97 100 62 13 10 60 98 111 100 121 62 13 10 9 60 110 111 115 99 114 105 112 116 62 60 109 101 116 97 32 104 116 116 112 45 101 113 117 105 118 61 34 114 101 102 114 101 115 104 34 32 99 111 110 116 101 110 116 61 34 48 59 117 114 108 61 104 116 116 112 58 47 47 119 119 119 46 98 97 105 100 117 46 99 111 109 47 34 62 60 47 110 111 115 99 114 105 112 116 62 13 10 60 47 98 111 100 121 62 13 10 60 47 104 116 109 108 62]
true

----------

url = https://httpbin.org/status/418
[10 32 32 32 32 45 61 91 32 116 101 97 112 111 116 32 93 61 45 10 10 32 32 32 32 32 32 32 95 46 46 46 46 95 10 32 32 32 32 32 46 39 32 32 95 32 95 32 96 46 10 32 32 32 32 124 32 46 34 96 32 94 32 96 34 46 32 95 44 10 32 32 32 32 92 95 59 96 34 45 45 45 34 96 124 47 47 10 32 32 32 32 32 32 124 32 32 32 32 32 32 32 59 47 10 32 32 32 32 32 32 92 95 32 32 32 32 32 95 47 10 32 32 32 32 32 32 32 32 96 34 34 34 96 10]
[10 32 32 32 32 45 61 91 32 116 101 97 112 111 116 32 93 61 45 10 10 32 32 32 32 32 32 32 95 46 46 46 46 95 10 32 32 32 32 32 46 39 32 32 95 32 95 32 96 46 10 32 32 32 32 124 32 46 34 96 32 94 32 96 34 46 32 95 44 10 32 32 32 32 92 95 59 96 34 45 45 45 34 96 124 47 47 10 32 32 32 32 32 32 124 32 32 32 32 32 32 32 59 47 10 32 32 32 32 32 32 92 95 32 32 32 32 32 95 47 10 32 32 32 32 32 32 32 32 96 34 34 34 96 10]
true

----------

[10 32 32 32 32 45 61 91 32 116 101 97 112 111 116 32 93 61 45 10 10 32 32 32 32 32 32 32 95 46 46 46 46 95 10 32 32 32 32 32 46 39 32 32 95 32 95 32 96 46 10 32 32 32 32 124 32 46 34 96 32 94 32 96 34 46 32 95 44 10 32 32 32 32 92 95 59 96 34 45 45 45 34 96 124 47 47 10 32 32 32 32 32 32 124 32 32 32 32 32 32 32 59 47 10 32 32 32 32 32 32 92 95 32 32 32 32 32 95 47 10 32 32 32 32 32 32 32 32 96 34 34 34 96 10]
true

prettyyjnic avatar Aug 26 '16 02:08 prettyyjnic

@prettyyjnic I got it. This is because the default Accept-Encoding header is gzip, deflate:

https://github.com/mozillazg/request/blob/master/headers.go#L6

var DefaultHeaders = map[string]string{
    "Connection":      "keep-alive",
    "Accept-Encoding": "gzip, deflate",
    "Accept":          "*/*",
    "User-Agent":      DefaultUserAgent,
}

It works as expected. You can change Accept-Encoding header if you want.

mozillazg avatar Aug 26 '16 14:08 mozillazg

@mozillazg ok, thanks

prettyyjnic avatar Aug 29 '16 02:08 prettyyjnic

@prettyyjnic You're welcome.

mozillazg avatar Aug 29 '16 12:08 mozillazg