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
Switch to argument rather than option for URL
master
1 parent
f38b0e6
commit
5f957b6ed5987556a41086d9ca7b65a54f84e5f8
Mark George
authored
on 12 May 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 `short:"H" 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" choice:"GET" choice:"POST" choice:"PUT" choice:"DELETE" choice:"PATCH" choice:"HEAD" choice:"OPTIONS" description:"HTTP method for request." default:"GET"` Body string `short:"b" long:"body" description:"Request body to send."` URL string `positional-arg-name:"URL" description:"The URL to send the request to." required:"true"` Args struct { URL string } `positional-args:"yes" required:"yes"` } func (opts *ClientCommand) Execute(args []string) error { sendRequest(opts) return nil } func sendRequest(opts *ClientCommand) { client := http.Client{} request, _ := http.NewRequest(opts.Method, opts.Args.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() showResponse(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:"url" description:"The URL to send the request to." required:"true"` Headers []string `short:"H" 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" choice:"GET" choice:"POST" choice:"PUT" choice:"DELETE" choice:"PATCH" choice:"HEAD" choice:"OPTIONS" 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() showResponse(response) } }
Show line notes below