golang 获取api 数据
直接上代码 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |