简体   繁体   中英

Reading non-utf8 encoded data from a network call in golang

I am trying to read bytes from http response body in golang. My problem is that the response body is encoded using ISO-8859-1. I want to read the response body in the same encoding and write the contents to a file in the ISO-8859-1 encoding.

Is there a way using which I can accomplish this? I don't want to convert the data into UTF-8 at all.

将响应主体作为字节片读取和写入, []byte是一种不透明的数据类型。

Here is a good read about encoding, which you might benefit from.

You are seemingly assuming Go decodes the raw bytes it receives when it performs a request. It does not.

Take this example:

package main

import (
    "io"
    "log"
    "net/http"
    "os"
)

func main() {
    // We perform a request to a Latin-1 encoded page
    resp, err := http.Get("http://andrew.triumf.ca/multilingual/samples/german.meta.html")
    if err != nil {
        log.Fatalln(err)
    }
    //     
    f, err := os.Create("/tmp/latin1")
    defer f.Close()
    if err != nil {
        log.Fatalln(err)
    }
    io.Copy(f, resp.Body)
}

In the documentation, you can read that resp.Body conforms to the io.ReadCloser interface, which allows you to read the raw bytes and stream them to a file.

Once we run this code, this is the output of file -i /tmp/latin1 :

/tmp/latin1: text/html; charset=iso-8859-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM