diff --git a/cli.go b/cli.go index 586f73b..b2ca204 100644 --- a/cli.go +++ b/cli.go @@ -92,6 +92,15 @@ func main() { return nil }, }, + { + Name: "patch", + Usage: "Send a PATCH Request", + Flags: postFlags, + Action: func(c *cli.Context) error { + mets.Patchbasic(c) + return nil + }, + }, } err := app.Run(os.Args) if err != nil { diff --git a/methods/patch.go b/methods/patch.go new file mode 100644 index 0000000..d6c5af6 --- /dev/null +++ b/methods/patch.go @@ -0,0 +1,35 @@ +package methods + +import ( + "bytes" + "fmt" + "net/http" + + "github.com/urfave/cli" +) + +//Patchbasic sends a basic PATCH request +func Patchbasic(c *cli.Context) { + url := c.String("url") + 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")) + 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(s) +}