go 发送post请求

来源:转载

package core import ( "net/http" "net/url" "strings" "encoding/json" "io/ioutil" "fmt" ) /* *发送GET请求 * */ func UrlGet()(result map[string]interface{}){ return nil } /* *发送post请求 *@param apiUrl api地址 *@param postParam post参数 *@param result map格式json数据, err error对象 */ func UrlPost(apiUrl string,postParam map[string]string)(result map[string]interface{}, err error){ postValue := url.Values{} for key, value := range postParam{ postValue.Set(key, value) } fmt.Println("<POST>" + apiUrl) fmt.Println("post param : " + postValue.Encode()) response, err := http.Post(apiUrl, "application/x-www-form-urlencoded", strings.NewReader(postValue.Encode())) obj := make(map[string]interface{}) if err != nil{ return nil, err } text, err2 := ioutil.ReadAll(response.Body) response.Body.Close() if err2 != nil{ return nil, err2 } err3 := json.Unmarshal(text, &obj) return obj, err3 }



分享给朋友:
您可能感兴趣的文章:
随机阅读: