1 Star 0 Fork 1

常清静矣/golang-100

forked from 蓝桥云课/golang-100 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
047-response.go 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
konroyliu 提交于 2021-01-11 10:51 . golang-100 answer
package main
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
)
func main() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Date", "Wed, 01 Jan 2020 00:00:00 GMT")
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello world"))
}))
defer ts.Close()
resp, err := http.Get(ts.URL)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
// DumpResponse returns wire representation
// of the http response
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatal(err)
}
// %q is used here to make the testable example work
// use %s to print in multiple lines:
// HTTP/1.1 200 OK
// Content-Length: 11
// Content-Type: text/plain; charset=utf-8
// Date: Wed, 01 Jan 2020 00:00:00 GMT
//
// hello world
fmt.Printf("%q", dump)
// Output:
// "HTTP/1.1 200 OK\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 01 Jan 2020 00:00:00 GMT\r\n\r\nhello world"
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/syongaaa/golang-100.git
git@gitee.com:syongaaa/golang-100.git
syongaaa
golang-100
golang-100
master

搜索帮助