GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
1
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
httpcat
Browse code
URI -> URL
Closes
#4
master
1 parent
2e7b39a
commit
68a1e47e4cd555e240881b1f81079d3ddb82c1c3
Mark George
authored
on 16 Apr 2022
Patch
Showing
1 changed file
src/client.go
Ignore Space
Show notes
View
src/client.go
/** Author: Mark George <mark.george@otago.ac.nz> License: Zero-Clause BSD License */ package main import ( "fmt" "net/http" "os" "strings" ) type ClientCommand struct { Headers []string `long:"header" description:"A header to add to request in the form name:value. Use multiple times for multiple headers."` Method string `short:"m" long:"method" description:"HTTP method for request" default:"GET"` URL string `short:"u" long:"url" description:"The URL to send the request to" required:"true"` Body string `short:"b" long:"body" description:"Request body to send"` } func (opts *ClientCommand) Execute(args []string) error { sendRequest(opts) return nil } func sendRequest(opts *ClientCommand) { client := http.Client{} request, _ := http.NewRequest(opts.Method, opts.URL, strings.NewReader(opts.Body)) // add extra headers if specified if len(opts.Headers) > 0 { for _, header := range opts.Headers { if name, value, err := parseHeader(header); err == nil { if globalOptions.Verbose { fmt.Println("Adding request header - " + name + ": " + value) } request.Header.Add(name, value) } else { fmt.Println("The following header is not in the correct format: " + header + "\nCorrect format is: 'name:value'") os.Exit(1) } } } if response, err := client.Do(request); err != nil { fmt.Println("Could not connect to server") } else { defer response.Body.Close() // show response if globalOptions.BodiesOnly { showResponseBody(response) } else if globalOptions.HeadersOnly { showResponseHeaders(response) } else { showEntireResponse(response) } } }
/** Author: Mark George <mark.george@otago.ac.nz> License: Zero-Clause BSD License */ package main import ( "fmt" "net/http" "os" "strings" ) type ClientCommand struct { URL string `short:"u" long:"uri" description:"The URI to send the request to" required:"true"` Headers []string `long:"header" description:"A header to add to request in the form name:value. Use multiple times for multiple headers."` Method string `short:"m" long:"method" description:"HTTP method for request" default:"GET"` Body string `short:"b" long:"body" description:"Request body to send"` } func (opts *ClientCommand) Execute(args []string) error { sendRequest(opts) return nil } func sendRequest(opts *ClientCommand) { client := http.Client{} request, _ := http.NewRequest(opts.Method, opts.URL, strings.NewReader(opts.Body)) // add extra headers if specified if len(opts.Headers) > 0 { for _, header := range opts.Headers { if name, value, err := parseHeader(header); err == nil { if globalOptions.Verbose { fmt.Println("Adding request header - " + name + ": " + value) } request.Header.Add(name, value) } else { fmt.Println("The following header is not in the correct format: " + header + "\nCorrect format is: 'name:value'") os.Exit(1) } } } if response, err := client.Do(request); err != nil { fmt.Println("Could not connect to server") } else { defer response.Body.Close() // show response if globalOptions.BodiesOnly { showResponseBody(response) } else if globalOptions.HeadersOnly { showResponseHeaders(response) } else { showEntireResponse(response) } } }
Show line notes below