| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package net
- import (
- "encoding/json"
- "fmt"
- "time"
- "github.com/go-resty/resty/v2"
- )
- func HttpGet(url string) []byte {
- client := resty.New().SetTimeout(time.Second * 3)
- resp, err := client.R().
- EnableTrace().
- Get(url)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- return resp.Body()
- }
- func HttpPost(url string, body interface{}) []byte {
- resp, err := resty.New().SetTimeout(time.Second*3).R().
- SetHeader("Content-Type", "application/json").
- SetBody(body).
- Post(url)
- if err != nil {
- fmt.Println(err)
- return nil
- }
- return resp.Body()
- }
- func HttpGetJSON(url string, response interface{}) (err error) {
- resp := HttpGet(url)
- err = json.Unmarshal(resp, response)
- return
- }
- func HttpPostJSON(url string, request, response interface{}) (err error) {
- body, err := json.Marshal(request)
- if err != nil {
- return
- }
- resp := HttpPost(url, body)
- err = json.Unmarshal(resp, response)
- return
- }
|