Skip to content

Commit

Permalink
add HeaderToString for req and resp
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Aug 9, 2022
1 parent cbf1b33 commit 85a3582
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
13 changes: 0 additions & 13 deletions pkg/util/util.go

This file was deleted.

11 changes: 11 additions & 0 deletions req.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package req

import (
"bytes"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -145,3 +146,13 @@ func cloneMap(h map[string]string) map[string]string {
}
return m
}

// convertHeaderToString converts http header to a string.
func convertHeaderToString(h http.Header) string {
if h == nil {
return ""
}
buf := new(bytes.Buffer)
h.Write(buf)
return buf.String()
}
5 changes: 5 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func (r *Request) TraceInfo() TraceInfo {
return ti
}

// HeaderToString get all header as string.
func (r *Request) HeaderToString() string {
return convertHeaderToString(r.Headers)
}

// SetURL set the url for request.
func (r *Request) SetURL(url string) *Request {
r.RawURL = url
Expand Down
8 changes: 8 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,11 @@ func (r *Response) GetHeaderValues(key string) []string {
}
return r.Header.Values(key)
}

// HeaderToString get all header as string.
func (r *Response) HeaderToString() string {
if r.Response == nil {
return ""
}
return convertHeaderToString(r.Header)
}

0 comments on commit 85a3582

Please sign in to comment.