Request

GET请求

  1. import (
  2. "fmt"
  3. "io/ioutil"
  4. "net/http"
  5. )
  6. func main() {
  7. resp, err := http.Get("http://httpbin.org/ip")
  8. if err != nil {
  9. fmt.Errorf("%s", err.Error())
  10. return
  11. }
  12. defer resp.Body.Close()
  13. body, err := ioutil.ReadAll(resp.Body)
  14. if err != nil {
  15. fmt.Errorf("%s", err.Error())
  16. return
  17. }
  18. fmt.Println(string(body))
  19. }

Post请求

  1. func main() {
  2. url := "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
  3. payload := strings.NewReader("doctype=json&i=好好学习 天天向上")
  4. req, err := http.NewRequest("POST", url, payload)
  5. if err != nil {
  6. fmt.Errorf("%s", err.Error())
  7. return
  8. }
  9. req.Header.Add("content-type", "application/x-www-form-urlencoded")
  10. res, err := http.DefaultClient.Do(req)
  11. if err != nil {
  12. fmt.Errorf("%s", err.Error())
  13. return
  14. }
  15. defer res.Body.Close()
  16. body, err := ioutil.ReadAll(res.Body)
  17. if err != nil {
  18. fmt.Errorf("%s", err.Error())
  19. return
  20. }
  21. fmt.Println(string(body))
  22. }

自定义request请求

  1. func main() {
  2. query := map[string]string{"location": "北京",
  3. "ak": "VAuehGLIw7lW6ovwpnKboM3I", "output": "json"}
  4. u, _ := url.Parse("http://api.map.baidu.com/telematics/v3/weather")
  5. q := u.Query()
  6. for k, v := range query {
  7. q.Set(k, v)
  8. }
  9. u.RawQuery = q.Encode()
  10. req, err := http.NewRequest("GET", u.String(), nil)
  11. if err != nil {
  12. fmt.Errorf("%s", err.Error())
  13. return
  14. }
  15. resp, err := http.DefaultClient.Do(req)
  16. if err != nil {
  17. fmt.Errorf("%s", err.Error())
  18. return
  19. }
  20. defer resp.Body.Close()
  21. body, err := ioutil.ReadAll(resp.Body)
  22. if err != nil {
  23. fmt.Errorf("%s", err.Error())
  24. return
  25. }
  26. fmt.Println(string(body))
  27. }

  1. import (
  2. "bytes"
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8. type UserInfo struct {
  9. ApiKey string `json:"apiKey"`
  10. UserId string `json:"userId"`
  11. }
  12. type Perception struct {
  13. InputText map[string]string `json:"inputText,omitempty"`
  14. InputImage map[string]string `json:"inputImage,omitempty"`
  15. SelfInfo map[string]*RequestLocation `json:"selfInfo,omitempty"`
  16. }
  17. type RequestLocation struct {
  18. City string `json:"city,omitempty"`
  19. Province string `json:"province,omitempty"`
  20. Street string `json:"street,omitempty"`
  21. }
  22. type RequestStruct struct {
  23. ReqType int `json:"reqType"`
  24. UserInfo *UserInfo `json:"userInfo"`
  25. Perception *Perception `json:"perception"`
  26. }
  27. type Test1 struct {
  28. Id string
  29. Age string
  30. Class string
  31. }
  32. type Test struct {
  33. Name string
  34. Test1 Test1
  35. }
  36. func main() {
  37. apiAddress := "http://openapi.tuling123.com/openapi/api/v2"
  38. requeStruct := &RequestStruct{0,
  39. &UserInfo{ApiKey: "231ae8807c384f41805802bdd4973638", UserId: "123456111"},
  40. &Perception{InputText: map[string]string{"text": "今天的天气如何"}, SelfInfo: map[string]*RequestLocation{"location": &RequestLocation{City: "北京"}}}}
  41. jsonByte, err := json.Marshal(requeStruct)
  42. if err != nil {
  43. fmt.Println(err.Error())
  44. } else {
  45. fmt.Println(string(jsonByte))
  46. }
  47. request, err := http.NewRequest("POST", apiAddress, bytes.NewBuffer(jsonByte))
  48. if err != nil {
  49. fmt.Println(err.Error())
  50. }
  51. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  52. client := http.Client{}
  53. resp, err := client.Do(request)
  54. if err != nil {
  55. fmt.Println(err.Error())
  56. return
  57. }
  58. defer resp.Body.Close()
  59. respBytes, err := ioutil.ReadAll(resp.Body)
  60. if err != nil {
  61. fmt.Println(err.Error())
  62. return
  63. }
  64. fmt.Println(string(respBytes))
  65. }

第三方

fasthttp

Go包——net - 图1

http Get请求

  1. import (
  2. "fmt"
  3. "github.com/valyala/fasthttp"
  4. "log"
  5. )
  6. func main() {
  7. c := fasthttp.Client{}
  8. statusCode, body, err := c.Get(nil, "http://httpbin.org/ip")
  9. if statusCode == fasthttp.StatusOK {
  10. fmt.Println(string(body))
  11. } else {
  12. log.Fatalf("%d:%s", statusCode, err.Error())
  13. }
  14. }