请求地址:/v1/User/updateEmail.json
请求方式:POST
接口版本: v1
接口描述:
/**
*
* 推荐尽量使用POST方式
* sdk接口都均以https方式调用,客户端需要验证服务器证书是否正常。
* cacert.pem为可信任根证书文件。用户请保持更新
*/
package APIStore
import (
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
//post访问方式(推荐)
//host 请求地址
//param 附加参数
func RequestPost(host string, param string) {
body := ioutil.NopCloser(strings.NewReader(param))
req, err := http.NewRequest("POST", host, body)
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(0)
}
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Add("Accept-Encoding", "UTF-8")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") //这个一定要加,不加form的值post不过去
//fmt.Printf("%+v\n", req) //看下发送的结构
client := &http.Client{}
resp, err := client.Do(req) //发送
if err != nil {
fmt.Println("error:", err)
return
}
defer resp.Body.Close() //一定要关闭resp.Body
data, err := ioutil.ReadAll(resp.Body)
}
//get方式请求
//host 请求地址
//param 附加参数
func RequestGet(host string, param string) {
req, err := http.NewRequest("GET", host+"?"+param, nil)
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(0)
}
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Add("Accept-Encoding", "UTF-8")
//fmt.Printf("%+v\n", req) //看下发送的结构
client := &http.Client{}
resp, err := client.Do(req) //发送
if err != nil {
fmt.Println("error:", err)
return
}
defer resp.Body.Close() //一定要关闭resp.Body
data, err := ioutil.ReadAll(resp.Body)
}
//获取内容
func getContent(requestType string) {
var url string="https://api-v2.1dq.com/v1/User/updateEmail.json";
var param string ="";
if requestType == "GET" {
RequestGet(url, param)
} else {
RequestPost(url, param)
}
}
}