100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > go对get post请求封装

go对get post请求封装

时间:2019-05-01 03:30:40

相关推荐

go对get post请求封装

一、参考1、Get请求封装2、Post请求封装二、按需调整1、GET请求2、POST请求三、其他四、json.Marshal()方法 特殊字符&被转义

一、参考

参考地址:go对get、post请求封装

1、Get请求封装

//Get http get methodfunc Get(url string, params map[string]string, headers map[string]string) (*http.Response, error) {//new requestreq, err := http.NewRequest("GET", url, nil)if err != nil {log.Println(err)return nil, errors.New("new request is fail ")}//add paramsq := req.URL.Query()if params != nil {for key, val := range params {q.Add(key, val)}req.URL.RawQuery = q.Encode()}//add headersif headers != nil {for key, val := range headers {req.Header.Add(key, val)}}//http clientclient := &http.Client{}log.Printf("Go GET URL : %s \n", req.URL.String())return client.Do(req)}

2、Post请求封装

//Post http post methodfunc Post(url string, body map[string]string, params map[string]string, headers map[string]string) (*http.Response, error) {//add post bodyvar bodyJson []bytevar req *http.Requestif body != nil {var err errorbodyJson, err = json.Marshal(body)if err != nil {log.Println(err)return nil, errors.New("http post body to json failed")}}req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyJson))if err != nil {log.Println(err)return nil, errors.New("new request is fail: %v \n")}req.Header.Set("Content-type", "application/json")//add paramsq := req.URL.Query()if params != nil {for key, val := range params {q.Add(key, val)}req.URL.RawQuery = q.Encode()}//add headersif headers != nil {for key, val := range headers {req.Header.Add(key, val)}}//http clientclient := &http.Client{}log.Printf("Go POST URL : %s \n", req.URL.String())return client.Do(req)}

二、按需调整

我不需要所有的response内容,我只要返回的res.Body数据

1、GET请求

//Get http get methodfunc Get(url string, params map[string]string, headers map[string]string) ([]byte, error) {//new requestreq, err := http.NewRequest("GET", url, nil)if err != nil {log.Println(err)return nil, errors.New("new request is fail ")}//add paramsq := req.URL.Query()if params != nil {for key, val := range params {q.Add(key, val)}req.URL.RawQuery = q.Encode()}//add headersif headers != nil {for key, val := range headers {req.Header.Add(key, val)}}//http clientclient := &http.Client{}log.Printf("Go GET URL : %s \n", req.URL.String())//发送请求res, err := client.Do(req)if err != nil {return nil, err}defer res.Body.Close() //一定要关闭res.Body//读取bodyresBody, err := ioutil.ReadAll(res.Body) //把 body 内容读入字符串 sif err != nil {return nil, err}return resBody, nil}

2、POST请求

//Post http post methodfunc Post(url string, body map[string]interface{}, params map[string]string, headers map[string]string) ([]byte, error) {//add post bodyvar bodyJson []bytevar req *http.Requestif body != nil {var err errorbodyJson, err = json.Marshal(body)if err != nil {log.Println(err)return nil, errors.New("http post body to json failed")}}req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyJson))if err != nil {log.Println(err)return nil, errors.New("new request is fail: %v \n")}req.Header.Set("Content-type", "application/json")//add paramsq := req.URL.Query()if params != nil {for key, val := range params {q.Add(key, val)}req.URL.RawQuery = q.Encode()}//add headersif headers != nil {for key, val := range headers {req.Header.Add(key, val)}}//http clientclient := &http.Client{}log.Printf("Go POST URL : %s \n", req.URL.String())//发送请求res, err := client.Do(req)if err != nil {return nil, err}defer res.Body.Close() //一定要关闭res.Body//读取bodyresBody, err := ioutil.ReadAll(res.Body) //把 body 内容读入字符串 sif err != nil {return nil, err}return resBody, nil}

三、其他

因为还在学习之中,如果内容有误,请宽容指出,不胜感激~

四、json.Marshal()方法 特殊字符&被转义

如果希望json.Marshal()特殊字符&不被转义为\u0026:请参考 golang 微信小程序获取二维码scene参数报错 invalid scene rid: f05f96ab-5382f139-14b13d2f

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。