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

Commit

Permalink
Basic POST methods
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 22, 2020
1 parent 997a8f0 commit 21ed02c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
70 changes: 52 additions & 18 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,39 +23,72 @@ 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",
Usage: "Add the Password",
},
}
postFlags := []cli.Flag{
cli.StringFlag{
Name: "p",
Value: "Password",
Usage: "Add the Password",
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",
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 {
if c.String("u") != "" && c.String("p") != "" {
mets.Getbasic(c)
} else if c.String("token") != "" {
mets.Getwtoken(c)
} else {
mets.Getreq(c)
tokavail := c.String("token")
uvail := c.String("u")
switch {
case tokavail != "":
mets.Authwtoken(c, "GET")
break
case uvail != "":
mets.Authbasic(c, "GET")
default:
mets.Basicreq(c)
}

return nil
},
},
{
Name: "post",
Usage: "Send a POST Request",

}
}
err := app.Run(os.Args)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/urfave/cli"
)

//Getreq sends a simple GET request to the url
func Getreq(c *cli.Context) error {
//Basicreq sends a simple GET request to the url
func Basicreq(c *cli.Context) error {
var url = c.String("url")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand All @@ -29,12 +29,12 @@ func Getreq(c *cli.Context) error {
return nil
}

//Getwtoken send a get request with the Token for Authorization Header
func Getwtoken(c *cli.Context) error {
//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("GET", url, nil)

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)
Expand All @@ -51,12 +51,12 @@ func Getwtoken(c *cli.Context) error {
return nil
}

//Getbasic helps you send a request with Basic Auth as Authorization Method
func Getbasic(c *cli.Context) error {
//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("GET", url, nil)
req, err := http.NewRequest(method, url, nil)
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
client := &http.Client{}
resp, err := client.Do(req)
Expand Down

0 comments on commit 21ed02c

Please sign in to comment.