首页 > Windows开发 > 详细

golang 获取api 数据

时间:2017-09-25 09:37:45      阅读:380      评论:0      收藏:0      [点我收藏+]

直接上代码


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


golang 获取api 数据

原文:http://bobo123.blog.51cto.com/3133979/1968220

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!