From 21ed02c5013ba10ce09b8bcddefc4c7fa7cc2aef Mon Sep 17 00:00:00 2001 From: athul Date: Wed, 22 Jan 2020 22:10:20 +0530 Subject: [PATCH 1/5] Basic POST methods --- cli.go | 70 +++++++++++++++++++++++++++++++++++++------------- methods/get.go | 18 ++++++------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/cli.go b/cli.go index 4883bf8..a56fe0e 100644 --- a/cli.go +++ b/cli.go @@ -1,10 +1,11 @@ package main import ( - mets "github.com/athul/pwcli/methods" - "github.com/urfave/cli" "log" "os" + + mets "github.com/athul/pwcli/methods" + "github.com/urfave/cli" ) func main() { @@ -13,7 +14,7 @@ func main() { app.Usage = "Test API endpoints without the hassle" app.Description = "Made with <3 by Postwoman Team" - myFlags := []cli.Flag{ + getFlags := []cli.Flag{ cli.StringFlag{ Name: "url", Value: "https://reqres.in/api/users", @@ -22,39 +23,72 @@ func main() { }, cli.StringFlag{ Name: "token", - Value: "Bearer Token", Usage: "Send the Request with Bearer Token", }, cli.StringFlag{ - Name: "u", - Value: "Username", - Usage: "Add the Username", - Required: true, + Name: "u", + Usage: "Add the Username", + }, + cli.StringFlag{ + Name: "p", + Usage: "Add the Password", }, + } + postFlags := []cli.Flag{ cli.StringFlag{ - Name: "p", - Value: "Password", - Usage: "Add the Password", + Name: "url", + Value: "https://reqres.in/api/users", + Usage: "The URL/Endpoint you want to check", Required: true, }, + cli.StringFlag{ + Name: "token", + Usage: "Send the Request with Bearer Token", + }, + cli.StringFlag{ + Name: "u", + Usage: "Add the Username", + }, + cli.StringFlag{ + Name: "p", + Usage: "Add the Password", + }, + cli.StringFlag{ + Name: "ctype", + Usage: "Change the Content Type", + }, + cli.StringFlag{ + Name: "body", + Usage: "Body of the Post Request", + }, } app.Commands = []cli.Command{ { Name: "get", Usage: "Send a GET request", - Flags: myFlags, + Flags: getFlags, Action: func(c *cli.Context) error { - if c.String("u") != "" && c.String("p") != "" { - mets.Getbasic(c) - } else if c.String("token") != "" { - mets.Getwtoken(c) - } else { - mets.Getreq(c) + tokavail := c.String("token") + uvail := c.String("u") + switch { + case tokavail != "": + mets.Authwtoken(c, "GET") + break + case uvail != "": + mets.Authbasic(c, "GET") + default: + mets.Basicreq(c) } + return nil }, }, + { + Name: "post", + Usage: "Send a POST Request", + + } } err := app.Run(os.Args) if err != nil { diff --git a/methods/get.go b/methods/get.go index 175cb89..b96e153 100644 --- a/methods/get.go +++ b/methods/get.go @@ -9,8 +9,8 @@ import ( "github.com/urfave/cli" ) -//Getreq sends a simple GET request to the url -func Getreq(c *cli.Context) error { +//Basicreq sends a simple GET request to the url +func Basicreq(c *cli.Context) error { var url = c.String("url") req, err := http.NewRequest("GET", url, nil) if err != nil { @@ -29,12 +29,12 @@ func Getreq(c *cli.Context) error { return nil } -//Getwtoken send a get request with the Token for Authorization Header -func Getwtoken(c *cli.Context) error { +//Authwtoken send a get request with the Token for Authorization Header +func Authwtoken(c *cli.Context, method string) error { var url = c.String("url") var bearer = "Bearer " + c.String("token") - req, err := http.NewRequest("GET", url, nil) - + req, err := http.NewRequest(method, url, nil) + req.Header.Add("Content-Type", "application/json") req.Header.Add("Authorization", bearer) client := &http.Client{} resp, err := client.Do(req) @@ -51,12 +51,12 @@ func Getwtoken(c *cli.Context) error { return nil } -//Getbasic helps you send a request with Basic Auth as Authorization Method -func Getbasic(c *cli.Context) error { +//Authbasic helps you send a request with Basic Auth as Authorization Method +func Authbasic(c *cli.Context, method string) error { un := c.String("u") pw := c.String("p") url := c.String("url") - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest(method, url, nil) req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) client := &http.Client{} resp, err := client.Do(req) From 0ed715ed94d8afb06a9ad4624666cb4ce9ed05a3 Mon Sep 17 00:00:00 2001 From: athul Date: Thu, 23 Jan 2020 19:34:03 +0530 Subject: [PATCH 2/5] Add Support for POST method --- cli.go | 17 ++++++++++------- methods/post.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 methods/post.go diff --git a/cli.go b/cli.go index a56fe0e..9f04876 100644 --- a/cli.go +++ b/cli.go @@ -55,6 +55,7 @@ func main() { }, cli.StringFlag{ Name: "ctype", + Value: "application/json", Usage: "Change the Content Type", }, cli.StringFlag{ @@ -69,13 +70,11 @@ func main() { Usage: "Send a GET request", Flags: getFlags, Action: func(c *cli.Context) error { - tokavail := c.String("token") - uvail := c.String("u") switch { - case tokavail != "": + case c.String("token") != "": mets.Authwtoken(c, "GET") break - case uvail != "": + case c.String("u") != "": mets.Authbasic(c, "GET") default: mets.Basicreq(c) @@ -85,10 +84,14 @@ func main() { }, }, { - Name: "post", + Name: "post", Usage: "Send a POST Request", - - } + Flags: postFlags, + Action: func(c *cli.Context) error { + mets.Postbasic(c) + return nil + }, + }, } err := app.Run(os.Args) if err != nil { diff --git a/methods/post.go b/methods/post.go new file mode 100644 index 0000000..3ab8028 --- /dev/null +++ b/methods/post.go @@ -0,0 +1,30 @@ +package methods + +import ( + "bytes" + "fmt" + "net/http" + + "github.com/urfave/cli" +) + +//Postbasic sends a basic POST request +func Postbasic(c *cli.Context) { + url := c.String("url") + var jsonStr = []byte(c.String("body")) + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) + req.Header.Set("X-Custom-Header", "myvalue") + req.Header.Set("Content-Type", c.String("ctype")) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + panic(err) + } + defer resp.Body.Close() + s := Formatresp(resp) + fmt.Println("response Status:", resp.Status) + fmt.Println("response Headers:", resp.Header) + //body, _ := ioutil.ReadAll(resp.Body) + fmt.Println("response Body:", s) +} From c703f7b6e16dadb59be6a79b99e6da948d497c13 Mon Sep 17 00:00:00 2001 From: athul Date: Fri, 24 Jan 2020 13:39:14 +0530 Subject: [PATCH 3/5] Added Support for Token and Basic Requests --- methods/post.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/methods/post.go b/methods/post.go index 3ab8028..acdfa1a 100644 --- a/methods/post.go +++ b/methods/post.go @@ -13,9 +13,18 @@ func Postbasic(c *cli.Context) { url := c.String("url") var jsonStr = []byte(c.String("body")) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) - req.Header.Set("X-Custom-Header", "myvalue") + //req.Header.Set("X-Custom-Header", "myvalue") req.Header.Set("Content-Type", c.String("ctype")) - + if c.String("token") != "" { + var bearer = "Bearer " + c.String("token") + req.Header.Add("Authorization", bearer) + } + if c.String("u") != "" && c.String("p") != "" { + un := c.String("u") + pw := c.String("p") + req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) + } + fmt.Print(req.Header) client := &http.Client{} resp, err := client.Do(req) if err != nil { From 8d552a52ac3a0676d070e60496f3a5caef07efeb Mon Sep 17 00:00:00 2001 From: athul Date: Fri, 24 Jan 2020 13:40:42 +0530 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=9B=A0Refactor:=20Reduced=20Functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli.go | 11 +-------- methods/fns.go | 5 ++++ methods/get.go | 62 +++++++++----------------------------------------- 3 files changed, 17 insertions(+), 61 deletions(-) diff --git a/cli.go b/cli.go index 9f04876..83f5fd0 100644 --- a/cli.go +++ b/cli.go @@ -70,16 +70,7 @@ func main() { Usage: "Send a GET request", Flags: getFlags, Action: func(c *cli.Context) error { - switch { - case c.String("token") != "": - mets.Authwtoken(c, "GET") - break - case c.String("u") != "": - mets.Authbasic(c, "GET") - default: - mets.Basicreq(c) - } - + mets.Basicreq(c) return nil }, }, diff --git a/methods/fns.go b/methods/fns.go index f676cad..80ef9f8 100644 --- a/methods/fns.go +++ b/methods/fns.go @@ -1,6 +1,7 @@ package methods import ( + "encoding/base64" "encoding/json" "fmt" "io/ioutil" @@ -25,3 +26,7 @@ func Formatresp(resp *http.Response) string { } return retbody } +func basicAuth(username, password string) string { + auth := username + ":" + password + return base64.StdEncoding.EncodeToString([]byte(auth)) +} diff --git a/methods/get.go b/methods/get.go index b96e153..5ba41a9 100644 --- a/methods/get.go +++ b/methods/get.go @@ -1,7 +1,6 @@ package methods import ( - "encoding/base64" "fmt" "log" "net/http" @@ -9,13 +8,23 @@ import ( "github.com/urfave/cli" ) -//Basicreq sends a simple GET request to the url +//Basicreq sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth func Basicreq(c *cli.Context) error { var url = c.String("url") req, err := http.NewRequest("GET", url, nil) if err != nil { return err } + if c.String("token") != "" { + var bearer = "Bearer " + c.String("token") + req.Header.Add("Authorization", bearer) + } + if c.String("u") != "" && c.String("p") != "" { + un := c.String("u") + pw := c.String("p") + req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) + } + fmt.Print(req.Header) client := &http.Client{} resp, err := client.Do(req) if err != nil { @@ -28,52 +37,3 @@ func Basicreq(c *cli.Context) error { fmt.Printf("\n\n %s", s) return nil } - -//Authwtoken send a get request with the Token for Authorization Header -func Authwtoken(c *cli.Context, method string) error { - var url = c.String("url") - var bearer = "Bearer " + c.String("token") - req, err := http.NewRequest(method, url, nil) - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", bearer) - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Println("Error on response.\n[ERRO] -", err) - } - defer resp.Body.Close() - s := Formatresp(resp) - if s != "" { - fmt.Printf("%s", s) - } else { - fmt.Print(resp) - } - return nil -} - -//Authbasic helps you send a request with Basic Auth as Authorization Method -func Authbasic(c *cli.Context, method string) error { - un := c.String("u") - pw := c.String("p") - url := c.String("url") - req, err := http.NewRequest(method, url, nil) - req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Println("Error on response.\n[ERRO] -", err) - } - defer resp.Body.Close() - s := Formatresp(resp) - if s != "" { - fmt.Printf("%s", s) - } else { - fmt.Print(resp) - } - - return nil -} -func basicAuth(username, password string) string { - auth := username + ":" + password - return base64.StdEncoding.EncodeToString([]byte(auth)) -} From 1894fbf8ed23323cb0d86242cb15177da1515a6f Mon Sep 17 00:00:00 2001 From: athul Date: Fri, 24 Jan 2020 19:37:08 +0530 Subject: [PATCH 5/5] =?UTF-8?q?=E2=9A=92Minor=20Refactors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli.go | 2 +- methods/fns.go | 2 +- methods/get.go | 11 ++++------- methods/post.go | 6 +----- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/cli.go b/cli.go index 83f5fd0..07866f3 100644 --- a/cli.go +++ b/cli.go @@ -70,7 +70,7 @@ func main() { Usage: "Send a GET request", Flags: getFlags, Action: func(c *cli.Context) error { - mets.Basicreq(c) + mets.Getbasic(c) return nil }, }, diff --git a/methods/fns.go b/methods/fns.go index 80ef9f8..debafd6 100644 --- a/methods/fns.go +++ b/methods/fns.go @@ -12,7 +12,7 @@ import ( ) // Formatresp formats the Response with Indents and Colors -func Formatresp(resp *http.Response) string { +func formatresp(resp *http.Response) string { body, err := ioutil.ReadAll(resp.Body) str := string(body) var obj map[string]interface{} diff --git a/methods/get.go b/methods/get.go index 5ba41a9..e1240d5 100644 --- a/methods/get.go +++ b/methods/get.go @@ -8,8 +8,8 @@ import ( "github.com/urfave/cli" ) -//Basicreq sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth -func Basicreq(c *cli.Context) error { +//Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth +func Getbasic(c *cli.Context) error { var url = c.String("url") req, err := http.NewRequest("GET", url, nil) if err != nil { @@ -24,16 +24,13 @@ func Basicreq(c *cli.Context) error { pw := c.String("p") req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) } - fmt.Print(req.Header) client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println("Error on response.\n[ERRO] -", err) } defer resp.Body.Close() - s := Formatresp(resp) - //log.Println() - fmt.Print(resp) - fmt.Printf("\n\n %s", s) + s := formatresp(resp) + fmt.Printf("\n%s", s) return nil } diff --git a/methods/post.go b/methods/post.go index acdfa1a..5238b18 100644 --- a/methods/post.go +++ b/methods/post.go @@ -24,16 +24,12 @@ func Postbasic(c *cli.Context) { pw := c.String("p") req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) } - fmt.Print(req.Header) client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() - s := Formatresp(resp) - fmt.Println("response Status:", resp.Status) - fmt.Println("response Headers:", resp.Header) - //body, _ := ioutil.ReadAll(resp.Body) + s := formatresp(resp) fmt.Println("response Body:", s) }