有关心情

Go Http 解析 text/plain

在没有任何框架的加持下,如果客户端(前端)传递过来的数据类型是:text/plain

// 请求头
Content-Type: text/plain;charset=UTF-8


Go 的解析方式是:

import (
    "io/ioutil"
    ...
)

func apiHandler(w http.ResponseWriter, r *http.Request) {
    content, _ := ioutil.ReadAll(r.Body)
    ...
}

使用 io/ioutil 包的好处在于,你不需要知道 content 长度为多少。否则处理方式为:

content := make([]byte, r.ContentLength)
r.Body.Read(content)

代码多了一丢丢。

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »