diff --git a/README.md b/README.md index f23a3b7..c588e48 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# PostWoman CLI [![Build Status](https://travis-ci.com/athul/pwcli.svg?token=udLtq6DyJs4Gxpze9nqX&branch=master)](https://travis-ci.com/athul/pwcli)[![Postwoman](https://img.shields.io/badge/Made_for-Postwoman-hex_color_code?logo=Postwoman)](https://postwoman.io) +# PostWoman CLI [![Build Status](https://travis-ci.com/athul/pwcli.svg?token=udLtq6DyJs4Gxpze9nqX&branch=master)](https://travis-ci.com/athul/pwcli)[![Postwoman](https://img.shields.io/badge/Made_for-Postwoman-hex_color_code?logo=Postwoman)](https://postwoman.io) [![Go Report Card](https://goreportcard.com/badge/github.com/athul/pwcli)](https://goreportcard.com/report/github.com/athul/pwcli) Use Postwoman's CLI direct from your terminal. # Installation @@ -33,8 +33,15 @@ Putting Simply: **Just pass the URL to the request method** - PUT : `pwcli put < -t/-u/-p > -c/--content type -b/--body ` - DELETE: `pwcli delete < -t/-u/-p > -c/--content type -b/--body ` -## Extra +**Content Types can be of** +`html` : `text/html` +`js` : `application/json`, +`xml` : `application/xml` +`plain` : `text/plain`, + + +#### Extra **SEND**: This can be used to test multiple endpoints from the `postwoman-collection.json` file. The output will only be the `statuscode`. RUN: `pwcli send ` OUTPUT: -![](/assets/send.png) \ No newline at end of file +![](/assets/send.png) diff --git a/assets/send.png b/assets/send.png index f3e9f4b..8072cda 100644 Binary files a/assets/send.png and b/assets/send.png differ diff --git a/cli.go b/cli.go index f1d658f..de5ad11 100644 --- a/cli.go +++ b/cli.go @@ -45,8 +45,8 @@ func main() { Usage: "Add the Password", }, cli.StringFlag{ - Name: "ctype, c", - Value: "application/json", + Name: "ctype, c", + //Value: "application/json", Usage: "Change the Content Type", }, cli.StringFlag{ diff --git a/go.mod b/go.mod index 6798ca7..555bbbe 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,6 @@ require ( github.com/fatih/color v1.9.0 github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e // indirect github.com/urfave/cli v1.22.2 + github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf + golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect ) diff --git a/go.sum b/go.sum index dadb30b..e2b18d7 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,15 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf h1:VA200mPTYh9FWY8zKX5ctXCtNk78HUez8ecTdsQGhoo= +github.com/yosssi/gohtml v0.0.0-20190915184251-7ff6f235ecaf/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/methods/delete.go b/methods/delete.go index aa4eb67..c54ae16 100644 --- a/methods/delete.go +++ b/methods/delete.go @@ -18,7 +18,7 @@ func Deletebasic(c *cli.Context) error { var jsonStr = []byte(c.String("body")) req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(jsonStr)) //req.Header.Set("X-Custom-Header", "myvalue") - req.Header.Set("Content-Type", c.String("ctype")) + req.Header.Set("Content-Type", Contenttypes[c.String("ctype")]) if c.String("token") != "" { var bearer = "Bearer " + c.String("token") req.Header.Add("Authorization", bearer) diff --git a/methods/fns.go b/methods/fns.go index d2f31a5..5cdde46 100644 --- a/methods/fns.go +++ b/methods/fns.go @@ -7,28 +7,47 @@ import ( "io/ioutil" "log" "net/http" + "strings" "github.com/TylerBrock/colorjson" "github.com/fatih/color" + "github.com/yosssi/gohtml" ) // Formatresp formats the Response with Indents and Colors func formatresp(resp *http.Response) string { + var retbody string + heads := fmt.Sprint(resp.Header) c := color.New(color.FgCyan, color.Bold) magenta := color.New(color.FgHiMagenta) yellow := color.New(color.FgHiYellow) body, err := ioutil.ReadAll(resp.Body) str := string(body) - var obj map[string]interface{} - json.Unmarshal([]byte(str), &obj) - f := colorjson.NewFormatter() - f.Indent = 6 - s, _ := f.Marshal(obj) - for key, value := range resp.Header { - c.Print(key, " : ") - magenta.Print(value, "\n") + if strings.Contains(heads, "json") { + var obj map[string]interface{} + json.Unmarshal([]byte(str), &obj) + f := colorjson.NewFormatter() + f.Indent = 6 + s, _ := f.Marshal(obj) + for key, value := range resp.Header { + c.Print(key, " : ") + magenta.Print(value, "\n") + } + retbody = yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(s)) + } else if strings.Contains(heads, "xml") || strings.Contains(heads, "html") || strings.Contains(heads, "plain") { + for key, value := range resp.Header { + c.Print(key, " : ") + magenta.Print(value, "\n") + } + var s string + if strings.Contains(heads, "plain") { + s = str + } else { + s = c.Sprint(gohtml.Format(str)) + } + retbody = yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", s) + } - retbody := yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(s)) if err != nil { log.Println("Error on response.\n[ERRO] -", err) } @@ -39,3 +58,11 @@ func basicAuth(username, password string) string { auth := username + ":" + password return base64.StdEncoding.EncodeToString([]byte(auth)) } + +//Contenttypes can be used in Place for ctypes +var Contenttypes = map[string]string{ + "html": "text/html", + "js": "application/json", + "xml": "application/xml", + "plain": "text/plain", +} diff --git a/methods/patch.go b/methods/patch.go index 7394fc6..e4f62de 100644 --- a/methods/patch.go +++ b/methods/patch.go @@ -19,7 +19,7 @@ func Patchbasic(c *cli.Context) { var jsonStr = []byte(c.String("body")) req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr)) //req.Header.Set("X-Custom-Header", "myvalue") - req.Header.Set("Content-Type", c.String("ctype")) + req.Header.Set("Content-Type", Contenttypes[c.String("ctype")]) if c.String("token") != "" { var bearer = "Bearer " + c.String("token") req.Header.Add("Authorization", bearer) diff --git a/methods/post.go b/methods/post.go index 74d369b..7fbde1e 100644 --- a/methods/post.go +++ b/methods/post.go @@ -19,7 +19,8 @@ func Postbasic(c *cli.Context) { 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")) + req.Header.Set("Content-Type", Contenttypes[c.String("ctype")]) + fmt.Print(Contenttypes[c.String("ctype")] + "\n") if c.String("token") != "" { var bearer = "Bearer " + c.String("token") req.Header.Add("Authorization", bearer) @@ -29,6 +30,7 @@ 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 { diff --git a/methods/put.go b/methods/put.go index bf6e934..f11c993 100644 --- a/methods/put.go +++ b/methods/put.go @@ -18,7 +18,7 @@ func Putbasic(c *cli.Context) error { var jsonStr = []byte(c.String("body")) req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr)) //req.Header.Set("X-Custom-Header", "myvalue") - req.Header.Set("Content-Type", c.String("ctype")) + req.Header.Set("Content-Type", Contenttypes[c.String("ctype")]) if c.String("token") != "" { var bearer = "Bearer " + c.String("token") req.Header.Add("Authorization", bearer) diff --git a/methods/send.go b/methods/send.go index 6fa61ec..12adaae 100644 --- a/methods/send.go +++ b/methods/send.go @@ -63,16 +63,20 @@ func ReadCollection(c *cli.Context) { } } -func request(c []Colls, i int) { +func request(c []Colls, i int) error { colors := color.New(color.FgHiRed, color.Bold) + fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path) + // if err != nil { + // fmt.Printf("%s\n", err.Error()) + // return nil + //} if c[0].Request[i].Method == "GET" { out, err := getsend(c, i, "GET") if err != nil { fmt.Print(err) } methods := color.HiYellowString(c[0].Request[i].Method) - fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path) - fmt.Printf("%s \t%s\t%s", fURL, methods, out) + fmt.Printf("%s |\t%s |\t%s |\t%s", color.HiGreenString(c[0].Request[i].Name), fURL, methods, out) } else { out, err := sendpopa(c, i, c[0].Request[i].Method) if err != nil { @@ -82,11 +86,12 @@ func request(c []Colls, i int) { fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path) fmt.Printf("%s |\t%s |\t%s |\t%s", color.HiGreenString(c[0].Request[i].Name), fURL, methods, out) } - + return nil } func getsend(c []Colls, ind int, method string) (string, error) { color := color.New(color.FgCyan, color.Bold) var url = c[0].Request[ind].URL + c[0].Request[ind].Path + //fmt.Print(url + " ") req, err := http.NewRequest(method, url, nil) if err != nil { return "", err