首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >go实现http请求

go实现http请求

作者头像
公众号-利志分享
发布于 2022-04-25 08:20:50
发布于 2022-04-25 08:20:50
1.6K0
举报
文章被收录于专栏:利志分享利志分享

//go发送http的get请求 package main import ( "fmt" "io/ioutil" //io模块 "net/http" //http服务模块 ) func main() { //go 发送http的get请求 //response, err := http.Get("http://www.baidu.com") response, err := http.Get("http://o.baidu.com") //如果发送失败 if err != nil { //handle error fmt.Println("发送get请求报错了。") } //这里是析构函数关闭body请求 defer response.Body.Close() //读取响应的body body, _ := ioutil.ReadAll(response.Body) //打印响应的body fmt.Println(string(body)) } -------------------------------------------------- //go发送http的post请求 package main import ( "bytes" "fmt" "io/ioutil" "net/http" ) func main() { //http post 请求 body := "{\"action\":20}" res, err := http.Post("https://passport.baidu.com/v2/api/?login", "application/json:charset=utf-8", bytes.NewBuffer([]byte(body))) if err != nil { fmt.Println("Fatal error", err.Error()) } defer res.Body.Close() content, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println("Fatal error", err.Error()) } fmt.Println(string(content)) }

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-09-13,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档