Skip to content

Commit

Permalink
Hide response headers per default; Add flag -i for showing headers (#5
Browse files Browse the repository at this point in the history
)

* Flag -i/--include now triggers inclusion of HTTP headers in output

The default is now to not print the headers. This complies with the
default behaviour of curl.
  • Loading branch information
makkes authored and bamarni committed Jul 8, 2019
1 parent cca3f4a commit fb4b1be
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pkg/cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
func NewHTTPCommand() *cobra.Command {
var method, data string
var headers []string
var includeHeaders bool

cmd := &cobra.Command{
Use: "http <path>",
Expand Down Expand Up @@ -61,23 +62,26 @@ func NewHTTPCommand() *cobra.Command {
}
defer resp.Body.Close()

return dumpResponse(resp)
return dumpResponse(resp, includeHeaders)
},
}

cmd.Flags().BoolVarP(&includeHeaders, "include", "i", false, "Include HTTP headers in the output")
cmd.Flags().StringVarP(&data, "data", "d", "", "HTTP POST data")
cmd.Flags().StringVarP(&method, "request", "X", "GET", "Specify request command to use")
cmd.Flags().StringSliceVarP(&headers, "header", "H", nil, "Pass custom header(s) to server")

return cmd
}

func dumpResponse(resp *http.Response) error {
respDump, err := httputil.DumpResponse(resp, false)
if err != nil {
return err
func dumpResponse(resp *http.Response, includeHeaders bool) error {
if includeHeaders {
respDump, err := httputil.DumpResponse(resp, false)
if err != nil {
return err
}
fmt.Println(string(respDump))
}
fmt.Println(string(respDump))
_, err = io.Copy(os.Stdout, resp.Body)
_, err := io.Copy(os.Stdout, resp.Body)
return err
}

0 comments on commit fb4b1be

Please sign in to comment.