加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 获取api 数据

发布时间:2020-12-16 18:04:51 所属栏目:大数据 来源:网络整理
导读:直接上代码 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.P

直接上代码


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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读