Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
🔀Merge pull request #9 from athul/ctypes
Browse files Browse the repository at this point in the history
Support for Ctypes , HTML and XML responses
  • Loading branch information
athul committed Feb 13, 2020
2 parents fe137f9 + db9a07a commit 6f797bf
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 22 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,8 +33,15 @@ Putting Simply: **Just pass the URL to the request method**
- PUT : `pwcli put <url> < -t/-u/-p > -c/--content type <content type> -b/--body <body>`
- DELETE: `pwcli delete <url> < -t/-u/-p > -c/--content type <content type> -b/--body <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 <PATH to postwoman collection.json>`
OUTPUT:
![](/assets/send.png)
![](/assets/send.png)
Binary file modified assets/send.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
2 changes: 1 addition & 1 deletion methods/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 36 additions & 9 deletions methods/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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",
}
2 changes: 1 addition & 1 deletion methods/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion methods/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions methods/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 6f797bf

Please sign in to comment.