1 Star 0 Fork 0

wukai/GoStudy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
http.go 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
Bruce.wu 提交于 2024-01-19 09:30 . feat: memberSite
package util
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
)
func CreateMemberSiteJsonRequest(url string, authHeader string, authAppId string, authSecret string, obj any) (*http.Request, error) {
body, err := json.Marshal(obj)
if err != nil {
slog.Error("Json marshal", "err", err)
return nil, err
}
auth, err := CreateClientAuthorization(authAppId, authSecret, body)
if err != nil {
slog.Error("Create client authorization", "err", err)
return nil, err
}
httpReq, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
slog.Error("Create request", "err", err)
return nil, err
}
httpReq.Header.Add("Content-Type", "application/json")
httpReq.Header.Add(authHeader, auth)
return httpReq, nil
}
func CreateMemberSiteGetRequest(url string, authHeader string, authAppId string, authSecret string) (*http.Request, error) {
auth, err := CreateClientAuthorization(authAppId, authSecret, []byte{})
if err != nil {
slog.Error("Create client authorization", "err", err)
return nil, err
}
if err != nil {
slog.Error("Create client authorization", "err", err)
return nil, err
}
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
httpReq.Header.Add(authHeader, auth)
return httpReq, nil
}
func LogHttpResponse(resp *http.Response) {
slog.Info("Response Headers:")
for hk, hv := range resp.Header {
slog.Info("", hk, hv)
}
respBodyBs, _ := io.ReadAll(resp.Body)
slog.Info("Response Body:")
slog.Info(string(respBodyBs))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/thinkerwolf/GoStudy.git
git@gitee.com:thinkerwolf/GoStudy.git
thinkerwolf
GoStudy
GoStudy
62820d0e2c88

搜索帮助