【三、http】go的http的响应信息

一、响应的信息

  • 状态码
  • 响应头
  • 编码

二、代码

cpp 复制代码
func requstGet(){
	r, err := http.Get("http://httpbin.org/get")
	if err != nil {
		fmt.Println("get err!")
	}

	defer r.Body.Close()

	//响应
	content, _ := io.ReadAll(r.Body)
	fmt.Printf("响应:%s\n", content)

	//状态码
	statusCode := r.StatusCode
	fmt.Printf("状态码:%d\n", statusCode)
	//状态描述信息
	statusCodeDis := r.Status
	fmt.Printf("状态描述:%s\n", statusCodeDis)
}

func main() {
	requstGet()
}

结果:

cpp 复制代码
响应:{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/1.1", 
    "X-Amzn-Trace-Id": "Root=1-6544e616-26ad5cd94f19ab1e7d856b50"
  }, 
  "origin": "120.244.60.192", 
  "url": "http://httpbin.org/get"
}

状态码:200
状态描述:200 OK

可以看到具体的响应信息

相关推荐
小红帽2.01 小时前
从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践
服务器·开发语言·golang
逢生博客1 小时前
Ubuntu 安装 gvm 管理 Go 语言开发环境
linux·ubuntu·golang·gvm
2501_915921438 小时前
iOS 应用上架多环境实战,Windows、Linux 与 Mac 的不同路径
android·ios·小程序·https·uni-app·iphone·webview
Cyclic100112 小时前
IOS购买订阅通知信息解析说明Java
java·开发语言·ios
00后程序员张12 小时前
iOS 应用上架常见问题与解决方案,多工具组合的实战经验
android·ios·小程序·https·uni-app·iphone·webview
rainFFrain1 天前
Boost搜索引擎项目(详细思路版)
网络·c++·http·搜索引擎
2501_916007471 天前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
猿究院--冯磊1 天前
计算机网络--HTTP协议
网络协议·计算机网络·http
bianshaopeng2 天前
ubuntu go 环境变量配置
开发语言·ubuntu·golang
元清加油2 天前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang