diff --git a/cli.go b/cli.go index 4883bf8..07866f3 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,36 +23,63 @@ 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", - Value: "Password", - Usage: "Add the Password", + Name: "p", + Usage: "Add the Password", + }, + } + postFlags := []cli.Flag{ + cli.StringFlag{ + 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", + Value: "application/json", + 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 { + mets.Getbasic(c) + return nil + }, + }, + { + Name: "post", + Usage: "Send a POST Request", + Flags: postFlags, 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) - } + mets.Postbasic(c) return nil }, }, diff --git a/methods/fns.go b/methods/fns.go index f676cad..debafd6 100644 --- a/methods/fns.go +++ b/methods/fns.go @@ -1,6 +1,7 @@ package methods import ( + "encoding/base64" "encoding/json" "fmt" "io/ioutil" @@ -11,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{} @@ -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 175cb89..e1240d5 100644 --- a/methods/get.go +++ b/methods/get.go @@ -1,7 +1,6 @@ package methods import ( - "encoding/base64" "fmt" "log" "net/http" @@ -9,71 +8,29 @@ import ( "github.com/urfave/cli" ) -//Getreq sends a simple GET request to the url -func Getreq(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 { return err } - 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) - return nil -} - -//Getwtoken send a get request with the Token for Authorization Header -func Getwtoken(c *cli.Context) error { - var url = c.String("url") - var bearer = "Bearer " + c.String("token") - req, err := http.NewRequest("GET", url, nil) - - req.Header.Add("Authorization", bearer) - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Println("Error on response.\n[ERRO] -", err) + if c.String("token") != "" { + var bearer = "Bearer " + c.String("token") + req.Header.Add("Authorization", bearer) } - defer resp.Body.Close() - s := Formatresp(resp) - if s != "" { - fmt.Printf("%s", s) - } else { - fmt.Print(resp) + if c.String("u") != "" && c.String("p") != "" { + un := c.String("u") + pw := c.String("p") + req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) } - return nil -} - -//Getbasic helps you send a request with Basic Auth as Authorization Method -func Getbasic(c *cli.Context) error { - un := c.String("u") - pw := c.String("p") - url := c.String("url") - req, err := http.NewRequest("GET", 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) - } - + s := formatresp(resp) + fmt.Printf("\n%s", s) return nil } -func basicAuth(username, password string) string { - auth := username + ":" + password - return base64.StdEncoding.EncodeToString([]byte(auth)) -} diff --git a/methods/post.go b/methods/post.go new file mode 100644 index 0000000..5238b18 --- /dev/null +++ b/methods/post.go @@ -0,0 +1,35 @@ +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")) + 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)) + } + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + panic(err) + } + defer resp.Body.Close() + s := formatresp(resp) + fmt.Println("response Body:", s) +}