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

Post #2

Merged
merged 5 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions cli.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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",
Expand All @@ -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
},
},
Expand Down
7 changes: 6 additions & 1 deletion methods/fns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package methods

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -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{}
Expand All @@ -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))
}
65 changes: 11 additions & 54 deletions methods/get.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,36 @@
package methods

import (
"encoding/base64"
"fmt"
"log"
"net/http"

"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))
}
35 changes: 35 additions & 0 deletions methods/post.go
Original file line number Diff line number Diff line change
@@ -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)
}