Skip to content

Commit

Permalink
httpx: 添加 AddHeader 方法 (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash authored Mar 4, 2024
1 parent ea42c17 commit 20b3765
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ekit
泛型工具库。

- [文档](https://ekit.gocn.vip/ekit/develop/guide/)
- [文档](https://doc.meoying.com//)

## 交流

Expand Down
5 changes: 5 additions & 0 deletions net/httpx/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (req *Request) Client(cli *http.Client) *Request {
return req
}

func (req *Request) AddHeader(key string, value string) *Request {
req.req.Header.Add(key, value)
return req
}

// AddParam 添加查询参数
// 这个方法性能不好,但是好用
func (req *Request) AddParam(key string, value string) *Request {
Expand Down
8 changes: 8 additions & 0 deletions net/httpx/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func TestRequest_AddParam(t *testing.T) {
assert.Equal(t, "http://localhost?key1=value1&key2=value2", req.req.URL.String())
}

func TestRequestAddHeader(t *testing.T) {
req := NewRequest(context.Background(),
http.MethodGet, "http://localhost").
AddHeader("head1", "val1").AddHeader("head1", "val2")
vals := req.req.Header.Values("head1")
assert.Equal(t, []string{"val1", "val2"}, vals)
}

type User struct {
Name string
}

0 comments on commit 20b3765

Please sign in to comment.