Skip to content

Commit

Permalink
feat: allow sending custom post requests
Browse files Browse the repository at this point in the history
ref #1

Signed-off-by: Marko Kungla <marko.kungla@gmail.com>
  • Loading branch information
mkungla committed Mar 17, 2022
1 parent c78116d commit 0c2e316
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion cmd-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"net/url"
"os"
"strings"

"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -585,7 +586,7 @@ func attachAPIGeneralCommmands(apicmd *cli.Command) {
apicmd.Subcommands = append(apicmd.Subcommands, []*cli.Command{
{
Name: "get",
Usage: "get issues a GET request to the specified API endpoint",
Usage: "send GET request to the specified API endpoint",
Category: "UTILS",
Action: func(ctx *cli.Context) error {
uri := ctx.Args().Get(0)
Expand All @@ -606,6 +607,33 @@ func attachAPIGeneralCommmands(apicmd *cli.Command) {
return nil
},
},
{
Name: "post",
Usage: "send POST request to the specified API endpoint",
Category: "UTILS",
Action: func(ctx *cli.Context) error {
uri := ctx.Args().Get(0)
pl := ctx.Args().Get(1)
if len(uri) == 0 {
return fmt.Errorf("%w: %s", ErrCommand, "provide endpoint as argument 1 e.g. /tip")
}
if len(pl) == 1 {
return fmt.Errorf("%w: %s", ErrCommand, "provide payload as argument 2")
}

u, err := url.ParseRequestURI(uri)
handleErr(err)

res, err := api.POST(callctx, u.Path, strings.NewReader(pl), u.Query(), nil)
handleErr(err)
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
handleErr(err)

printJSON(ctx, body)
return nil
},
},
{
Name: "head",
Usage: "head issues a HEAD request to the specified API endpoint",
Expand Down

0 comments on commit 0c2e316

Please sign in to comment.