網(wǎng)站首頁 編程語言 正文
通過go語言實現(xiàn)http請求
http.Post
import ( ?? ?"net/http" ?? ?"net/url" ) data := url.Values{"start":{"100"}, "hobby":{"xxxx"}} body := strings.NewReader(data.Encode()) resp, err := http.Post("127.0.0.1:9338", "application/x-www-form-urlencoded", body)
net/http包沒有封裝直接使用請求帶header的get或者post方法,所以,要想請求中帶header,只能使用NewRequest方法
http.NewRequest
客戶端:
import ( ?? ?"net/http" ?? ?"json" ?? ?"ioutil" ) type Student struct{ ?? ?id string ?? ?name string } type StudentReq struct{ ?? ?id string ?? ?name string } func main() { ?? ?stu := Student{ ?? ??? ?id:"2ed4tg5fe35fgty3yy6uh", ?? ??? ?name:"amber", ?? ?} ?? ?stu,err := json.Marshal(&stu) ?? ?reader := bytes.NewReader(stu) ?? ?request,err := http.NewRequest("POST", "http://192.168.1.12:8000/create", reader) ?? ?request.Header.Set("Content-Type", "application/json") ?? ?client:=&http.Client{} ?? ?response,err := client.Do(request) ?? ?defer response.Body.Close() ?? ?body,err := ioutil.ReadAll(response.Body) ?? ?fmt.Printf(string(body)) ?? ? ?? ?var stuReq StudentReq? ?? ?err = json.UnMarshal(body, &stuReq) ?? ?fmt.Println(json.MarshalIndent(stuReq)) }
解析:
- stu,err := json.Marshal(&stu):將stu對象改為json格式
- reader := bytes.NewReader(stu):所以將json改為byte格式,作為body傳給http請求
- request,err := http.NewRequest(“POST”, “http://192.168.1.12:8000/create”, reader):創(chuàng)建url
- response,err := client.Do(request):客戶端發(fā)起請求,接收返回值
- body,err := ioutil.ReadAll(response.Body):讀取body的值,類型是byte
- json.MarshalIndent(stuReq):修改json為標(biāo)準(zhǔn)格式
注意(坑):
1、header里的參數(shù)是Content-Type,不要寫成ContentType
2、【go http: read on closed response body 】如果發(fā)送的請求是分為2個func寫的,記住defer要在ioutil.ReadAll之后執(zhí)行,否則報錯
gorequest
這種方式適合在url里拼接參數(shù)使用param直接傳遞
"github.com/parnurzeal/gorequest" func main() { ?? ?resp, body, errs := gorequest.New().Post("http://127.0.0.1/create").Param("ip", "192.168.1.4").EndBytes() ?? ??? ?if errs != nil || resp.StatusCode >= 300 { ?? ??? ??? ?log.Errorf("fail to call api with errors %v, %+v", errs, body) ?? ??? ?} ?? ?var stuReq StudentReq? ?? ?err = json.UnMarshal(body, &stuReq) ?? ?fmt.Println(json.MarshalIndent(stuReq)) }
原文鏈接:https://blog.csdn.net/ambzheng/article/details/104483754
相關(guān)推薦
- 2022-11-17 淺析golang的依賴注入_Golang
- 2022-10-10 python正則表達(dá)式之re.match()與re.search()的用法及區(qū)別_python
- 2022-05-14 Python實現(xiàn)簡單的圖書管理系統(tǒng)_python
- 2024-02-25 關(guān)于數(shù)據(jù)庫時間為datetime,前臺出去數(shù)據(jù)為秒數(shù)的解決辦法
- 2022-09-30 Python?添加命令行參數(shù)步驟_python
- 2022-09-04 ffmpeg網(wǎng)頁視頻流m3u8?ts實現(xiàn)視頻下載_相關(guān)技巧
- 2022-12-22 python3中超級好用的日志模塊-loguru模塊使用詳解_python
- 2022-06-02 詳解Python?flask的前后端交互_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支