Skip to content

Commit

Permalink
httpx:增加error判断
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared-lu committed Nov 5, 2024
1 parent a4ff9fc commit 3b3698d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions net/httpx/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func NewRequest(ctx context.Context, method, url string) *Request {

// JSONBody 使用 JSON body
func (req *Request) JSONBody(val any) *Request {
if req.err != nil {
return req
}
req.req.Body = io.NopCloser(iox.NewJSONReader(val))
req.req.Header.Set("Content-Type", "application/json")
return req
Expand All @@ -50,13 +53,19 @@ func (req *Request) Client(cli *http.Client) *Request {
}

func (req *Request) AddHeader(key string, value string) *Request {
if req.err != nil {
return req
}
req.req.Header.Add(key, value)
return req
}

// AddParam 添加查询参数
// 这个方法性能不好,但是好用
func (req *Request) AddParam(key string, value string) *Request {
if req.err != nil {
return req
}
q := req.req.URL.Query()
q.Add(key, value)
req.req.URL.RawQuery = q.Encode()
Expand Down

0 comments on commit 3b3698d

Please sign in to comment.