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)) -}