1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| package main
import ( "bytes" "crypto/tls" "flag" "fmt" "io/ioutil" "net/http" "os" )
var ( s bool h bool t string b string resp *http.Response err error )
func init() { flag.BoolVar(&h, "h", false, "this help") flag.BoolVar(&s, "s", false, "Insecure SSL Skip Verify, default false")
flag.StringVar(&t, "u", "", "url") flag.StringVar(&b, "b", "", "'{\"key\":\"value\"}'") flag.Usage = usage }
func usage() { fmt.Fprintf(os.Stderr, `http POST application/json Usage: gohttppost [-h] [-u url] [-b body]
Options: `) flag.PrintDefaults() }
func main() { flag.Parse()
if h { flag.Usage() os.Exit(0) }
payloadBytes := []byte(b) body := bytes.NewReader(payloadBytes) req, err := http.NewRequest("POST", t, body) if err != nil { fmt.Println("err:", "http.NewRequest:", err) } req.Header.Set("Content-Type", "application/json")
if s { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := http.Client{Transport: tr} resp, err = client.Do(req)
} else { resp, err = http.DefaultClient.Do(req) }
if err != nil { fmt.Println("err:", "http client request:", err) } defer resp.Body.Close() fmt.Println("response Status:", resp.Status) resBody, _ := ioutil.ReadAll(resp.Body) fmt.Println("response Body:", string(resBody)) }
|