123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package services
- import (
- "bytes"
- "eta/eta_mini_api/utils"
- "io"
- "net/http"
- "time"
- )
- func HttpGet(url string) (body []byte, err error) {
- client := &http.Client{}
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return
- }
- nonce := utils.GetRandStringNoSpecialChar(16)
- timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
- signature := utils.GetSign(nonce, timestamp, utils.ETA_MINI_APPID, utils.ETA_MINI_APP_SECRET)
- //增加header选项
- req.Header.Add("Nonce", nonce)
- req.Header.Add("Timestamp", timestamp)
- req.Header.Add("Appid", utils.ETA_MINI_APPID)
- req.Header.Add("Signature", signature)
- req.Header.Set("Content-Type", "application/json")
- response, err := client.Do(req)
- if err != nil {
- return
- }
- defer response.Body.Close()
- body, err = io.ReadAll(response.Body)
- if err != nil {
- return
- }
- utils.FileLog.Info("result:" + string(body))
- return
- }
- func HttpPost(url string, postBody []byte) (body []byte, err error) {
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, bytes.NewReader(postBody))
- if err != nil {
- return
- }
- nonce := utils.GetRandStringNoSpecialChar(16)
- timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
- signature := utils.GetSign(nonce, timestamp, utils.ETA_MINI_APPID, utils.ETA_MINI_APP_SECRET)
- //增加header选项
- req.Header.Add("Nonce", nonce)
- req.Header.Add("Timestamp", timestamp)
- req.Header.Add("Appid", utils.ETA_MINI_APPID)
- req.Header.Add("Signature", signature)
- req.Header.Set("Content-Type", "application/json")
- response, err := client.Do(req)
- if err != nil {
- return
- }
- defer response.Body.Close()
- body, err = io.ReadAll(response.Body)
- if err != nil {
- return
- }
- utils.FileLog.Info("result:" + string(body))
- return
- }
|