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

Commit

Permalink
Add Support for POST method
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 23, 2020
1 parent 21ed02c commit 0ed715e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
17 changes: 10 additions & 7 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func main() {
},
cli.StringFlag{
Name: "ctype",
Value: "application/json",
Usage: "Change the Content Type",
},
cli.StringFlag{
Expand All @@ -69,13 +70,11 @@ func main() {
Usage: "Send a GET request",
Flags: getFlags,
Action: func(c *cli.Context) error {
tokavail := c.String("token")
uvail := c.String("u")
switch {
case tokavail != "":
case c.String("token") != "":
mets.Authwtoken(c, "GET")
break
case uvail != "":
case c.String("u") != "":
mets.Authbasic(c, "GET")
default:
mets.Basicreq(c)
Expand All @@ -85,10 +84,14 @@ func main() {
},
},
{
Name: "post",
Name: "post",
Usage: "Send a POST Request",

}
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Postbasic(c)
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions methods/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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"))

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
s := Formatresp(resp)
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
//body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", s)
}

0 comments on commit 0ed715e

Please sign in to comment.