Request
GET请求
import ("fmt""io/ioutil""net/http")func main() {resp, err := http.Get("http://httpbin.org/ip")if err != nil {fmt.Errorf("%s", err.Error())return}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Errorf("%s", err.Error())return}fmt.Println(string(body))}
Post请求
func main() {url := "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"payload := strings.NewReader("doctype=json&i=好好学习 天天向上")req, err := http.NewRequest("POST", url, payload)if err != nil {fmt.Errorf("%s", err.Error())return}req.Header.Add("content-type", "application/x-www-form-urlencoded")res, err := http.DefaultClient.Do(req)if err != nil {fmt.Errorf("%s", err.Error())return}defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)if err != nil {fmt.Errorf("%s", err.Error())return}fmt.Println(string(body))}
自定义request请求
func main() {query := map[string]string{"location": "北京","ak": "VAuehGLIw7lW6ovwpnKboM3I", "output": "json"}u, _ := url.Parse("http://api.map.baidu.com/telematics/v3/weather")q := u.Query()for k, v := range query {q.Set(k, v)}u.RawQuery = q.Encode()req, err := http.NewRequest("GET", u.String(), nil)if err != nil {fmt.Errorf("%s", err.Error())return}resp, err := http.DefaultClient.Do(req)if err != nil {fmt.Errorf("%s", err.Error())return}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Errorf("%s", err.Error())return}fmt.Println(string(body))}
import ("bytes""encoding/json""fmt""io/ioutil""net/http")type UserInfo struct {ApiKey string `json:"apiKey"`UserId string `json:"userId"`}type Perception struct {InputText map[string]string `json:"inputText,omitempty"`InputImage map[string]string `json:"inputImage,omitempty"`SelfInfo map[string]*RequestLocation `json:"selfInfo,omitempty"`}type RequestLocation struct {City string `json:"city,omitempty"`Province string `json:"province,omitempty"`Street string `json:"street,omitempty"`}type RequestStruct struct {ReqType int `json:"reqType"`UserInfo *UserInfo `json:"userInfo"`Perception *Perception `json:"perception"`}type Test1 struct {Id stringAge stringClass string}type Test struct {Name stringTest1 Test1}func main() {apiAddress := "http://openapi.tuling123.com/openapi/api/v2"requeStruct := &RequestStruct{0,&UserInfo{ApiKey: "231ae8807c384f41805802bdd4973638", UserId: "123456111"},&Perception{InputText: map[string]string{"text": "今天的天气如何"}, SelfInfo: map[string]*RequestLocation{"location": &RequestLocation{City: "北京"}}}}jsonByte, err := json.Marshal(requeStruct)if err != nil {fmt.Println(err.Error())} else {fmt.Println(string(jsonByte))}request, err := http.NewRequest("POST", apiAddress, bytes.NewBuffer(jsonByte))if err != nil {fmt.Println(err.Error())}request.Header.Set("Content-Type", "application/json;charset=UTF-8")client := http.Client{}resp, err := client.Do(request)if err != nil {fmt.Println(err.Error())return}defer resp.Body.Close()respBytes, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println(err.Error())return}fmt.Println(string(respBytes))}
第三方
fasthttp

http Get请求
import ("fmt""github.com/valyala/fasthttp""log")func main() {c := fasthttp.Client{}statusCode, body, err := c.Get(nil, "http://httpbin.org/ip")if statusCode == fasthttp.StatusOK {fmt.Println(string(body))} else {log.Fatalf("%d:%s", statusCode, err.Error())}}
