Skip to content

Commit 4474b5f

Browse files
author
王聪
committed
增加获取返回的header信息功能
1 parent 3ad233f commit 4474b5f

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/kplcloud/request
2+
3+
go 1.12
4+
5+
require (
6+
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
7+
gopkg.in/yaml.v2 v2.2.2
8+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2+
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
3+
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
4+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
6+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
10+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

request.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Result struct {
5353
contentType string
5454
err error
5555
statusCode int
56+
headers map[string][]string
5657

5758
decoder Decoder
5859
}
@@ -103,6 +104,10 @@ func (r Result) HttpStatusCode() int {
103104
return r.statusCode
104105
}
105106

107+
func (r Result) Headers() map[string][]string {
108+
return r.headers
109+
}
110+
106111
// StatusCode returns the HTTP status code of the request. (Only valid if no
107112
// error was returned.)
108113
func (r Result) StatusCode(statusCode *int) Result {
@@ -447,6 +452,7 @@ func (r *Request) transformResponse(resp *http.Response, req *http.Request) Resu
447452
statusCode: resp.StatusCode,
448453
decoder: decoder,
449454
err: r.transformUnstructuredResponseError(resp, req, body),
455+
headers: resp.Header,
450456
}
451457
}
452458

@@ -455,6 +461,7 @@ func (r *Request) transformResponse(resp *http.Response, req *http.Request) Resu
455461
contentType: contentType,
456462
statusCode: resp.StatusCode,
457463
decoder: decoder,
464+
headers: resp.Header,
458465
}
459466
}
460467

request_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func TestRequest_Do(t *testing.T) {
2727
t.Error("err", err.Error())
2828
}
2929
t.Log("success", res)
30+
31+
resp := NewRequest("https://www.iphpt.com/", "POST").
32+
Body([]byte(body)).Do()
33+
t.Log(resp.headers)
34+
3035
}
3136

3237
func TestRequest_HttpClient(t *testing.T) {

0 commit comments

Comments
 (0)