golang NewRequest/gorequest實(shí)現(xiàn)http請(qǐng)求的示例代碼
通過(guò)go語(yǔ)言實(shí)現(xiàn)http請(qǐng)求
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包沒(méi)有封裝直接使用請(qǐng)求帶header的get或者post方法,所以,要想請(qǐng)求中帶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對(duì)象改為json格式
- reader := bytes.NewReader(stu):所以將json改為byte格式,作為body傳給http請(qǐng)求
- request,err := http.NewRequest(“POST”, “http://192.168.1.12:8000/create”, reader):創(chuàng)建url
- response,err := client.Do(request):客戶端發(fā)起請(qǐng)求,接收返回值
- body,err := ioutil.ReadAll(response.Body):讀取body的值,類型是byte
- json.MarshalIndent(stuReq):修改json為標(biāo)準(zhǔn)格式
注意(坑):
1、header里的參數(shù)是Content-Type,不要寫(xiě)成ContentType
2、【go http: read on closed response body 】如果發(fā)送的請(qǐng)求是分為2個(gè)func寫(xiě)的,記住defer要在ioutil.ReadAll之后執(zhí)行,否則報(bào)錯(cuò)
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))
}到此這篇關(guān)于golang NewRequest/gorequest實(shí)現(xiàn)http請(qǐng)求的示例代碼的文章就介紹到這了,更多相關(guān)golang http請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang cron 定時(shí)器和定時(shí)任務(wù)的使用場(chǎng)景
Ticker是一個(gè)周期觸發(fā)定時(shí)的計(jì)時(shí)器,它會(huì)按照一個(gè)時(shí)間間隔往channel發(fā)送系統(tǒng)當(dāng)前時(shí)間,而channel的接收者可以以固定的時(shí)間間隔從channel中讀取事件,這篇文章主要介紹了Golang cron 定時(shí)器和定時(shí)任務(wù),需要的朋友可以參考下2022-09-09
golang rate令牌桶源碼分析實(shí)現(xiàn)方式
這篇文章主要介紹了golang rate令牌桶源碼分析實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Golang 空map和未初始化map的注意事項(xiàng)說(shuō)明
這篇文章主要介紹了Golang 空map和未初始化map的注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
VSCode安裝go相關(guān)插件失敗的簡(jiǎn)單解決方案
這篇文章主要給大家介紹了關(guān)于VSCode安裝go相關(guān)插件失敗的簡(jiǎn)單解決方案,VSCode是我們開(kāi)發(fā)go程序的常用工具,最近安裝的時(shí)候遇到了些問(wèn)題,需要的朋友可以參考下2023-07-07
詳解如何在Go中循環(huán)中使用Defer關(guān)鍵字示例詳解
這篇文章主要為大家介紹了詳解如何在Go中循環(huán)中使用Defer關(guān)鍵字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Golang 操作 Kafka 如何設(shè)置消息的失效時(shí)間
在使用 Golang 操作 Kafka 時(shí),你可以使用 Sarama 庫(kù)來(lái)設(shè)置消息的失效時(shí)間,這篇文章主要介紹了Golang操作Kafka設(shè)置消息的失效時(shí)間,需要的朋友可以參考下2023-06-06
Golang?WorkerPool線程池并發(fā)模式示例詳解
這篇文章主要為大家介紹了Golang?WorkerPool線程池并發(fā)模式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

