golang 獲取api 數(shù)據(jù)
直接上代码
type ApiRequest interface{}
//参数 e 一个空接口,f http请求连接, p 请求方式 参数为 “POST” 或 “GET”
func FromApiGetData(e *ApiRequest, f *PostUrl, p string) (string, error) {
jsons, errs := json.Marshal(e)
if errs != nil {
fmt.Println(errs.Error())
}
fmt.Println(string(jsons))
req, err := http.NewRequest(p, f.Url, bytes.NewBuffer([]byte(string(jsons))))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
//status := resp.Status
//headers := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
bodys := string(body)
return bodys, nil
}
注:空接口 在main 中定义 例如
type PostConLogin struct {
Email string `json:"email"`
Password string `json:"password"`
}
m := PostConLogin{Email: "1216562200@qq.com", Password: "123456"}; var v ApiRequest = m
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
100積分直接送
付費(fèi)專(zhuān)欄免費(fèi)學(xué)
大額優(yōu)惠券免費(fèi)領(lǐng)