Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Formatting headers - taking first header value (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
maorlx authored Oct 8, 2020
1 parent 52ad1e4 commit 2992896
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions wrappers/net/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ func updateResponseData(resp *http.Response, resource *protocol.Resource, metada
if metadataOnly {
return
}
headers, err := json.Marshal(resp.Header)
headers, err := formatHeaders(resp.Header)
if err == nil {
resource.Metadata["response_headers"] = string(headers)
resource.Metadata["response_headers"] = headers
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
Expand Down Expand Up @@ -431,9 +431,9 @@ func newReadCloser(body []byte, err error) io.ReadCloser {
}

func updateRequestData(req *http.Request, metadata map[string]string) {
headers, err := json.Marshal(req.Header)
headers, err := formatHeaders(req.Header)
if err == nil {
metadata["request_headers"] = string(headers)
metadata["request_headers"] = headers
}
if req.Body == nil {
return
Expand All @@ -450,3 +450,18 @@ func updateRequestData(req *http.Request, metadata map[string]string) {
}
}
}

// format HTTP headers to string - using first header value, ignoring the rest
func formatHeaders(headers http.Header) (string, error) {
headersToFormat := make(map[string]string)
for headerKey, headerValues := range headers {
if len(headerValues) > 0 {
headersToFormat[headerKey] = headerValues[0]
}
}
headersJson, err := json.Marshal(headersToFormat)
if err != nil {
return "", err
}
return string(headersJson), nil
}

0 comments on commit 2992896

Please sign in to comment.