Home >Backend Development >Golang >golang http set encoding

golang http set encoding

王林
王林Original
2023-05-13 09:22:061057browse

Programming language Golang is a programming language with fast development and outstanding performance. It is also an efficient and powerful programming language launched by Google in recent years. It has the characteristics of simple syntax, fast compilation, high running efficiency, and can achieve concurrency. Programming and developing network applications are also very convenient. In Golang, HTTP development is very common. HTTP requests can also handle Unicode strings. However, the problem of garbled characters when processing Chinese strings is a common problem. This article is written to solve this problem. .

Use the http package in Golang for network communication and server-side development. Using the http package can easily implement http request and response functions. At the same time, Golang's htt package also supports setting the encoding method of http requests and responses, which can solve the problem of Chinese encoding.

Set HTTP request encoding method

In an HTTP request, the main part of the request is the data represented by the media type specified by the "Content-Type" header field. For example: application/json, application/xml, etc. At the same time, the encoding method of the request can be specified by setting the "charset" parameter in the "Content-Type" header field.

The specific implementation is as follows:

package main

import (
    "io/ioutil"
    "net/http"
    "bytes"
)

func main() {
    url := "http://www.example.com/api"
    params := []byte(`{"username":"user","password":"pass"}`)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(params))
    if err != nil {
        // handle error
    }

    req.Header.Set("Content-Type", "application/json; charset=utf-8")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        // handle error
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
    }

    // handle response
}

In the above code, the request parameters are formatted as JSON and the "charset" of the "Content-Type" header field is set to "utf-8".

Set HTTP response encoding method

For the encoding method of HTTP response, you can also set the "charset" parameter in the "Content-Type" header field to specify the encoding method, as follows:

package main

import (
    "net/http"
)

func main() {
    content := "你好,世界"

    w := http.ResponseWriter
    w.Header().Set("Content-Type", "text/html;charset=utf-8")
    w.Write([]byte(content))
}

In the above code, use "http.ResponseWriter" to generate http response. Set the "charset" of the "Content-Type" header field to "utf-8" and output a string in UTF-8 encoding format.

Summary

Golang's HTTP development can easily solve the problem of Chinese encoding. You only need to set the "charset" parameter in the "Content-Type" header field. The encoding methods of Http requests and responses need to be consistent so that there will be no Chinese garbled problems.

The above is the detailed content of golang http set encoding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn