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

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
devblin committed Dec 8, 2021
2 parents 72595d6 + 8fc5452 commit e0c30de
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,27 @@ _(optional)_
|`html`|`text/html`|
|`xml`|`application/xml`|
|`plain`|`text/plain`|

### Include Arbitrary Headers
- `-H` or `--header` may be specified multiple times to include headers with the request.
- Example:
- `hopp-cli get -H 'X-Api-Key: foobar' -H 'X-Api-Secret: super_secret' https://example.com/api/v1/accounts`

### Providing a Request Body via stdin

In addition to `-b`/`--body`, you may provide a request body via stdin.
If you combine this method with the `-b` flag, the body provided with `-b` will be ignored.

**Example with Pipes**
```shell
$ echo '{"foo":"bar"}' | hopp-cli post -c js http://example.com
```
**Example with Redirection**
```shell
$ cat myrequest.json
{
"foo": "bar"
}

$ hopp-cli post -c js http://example.com <myrequest.json
```
10 changes: 9 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func main() {
Name: "p",
Usage: "Add the Password",
},
cli.StringSliceFlag{
Name: "header, H",
Usage: "Header to pass with the request. Can be used multiple times.",
},
}
genFlags := []cli.Flag{
cli.IntFlag{
Expand All @@ -61,7 +65,7 @@ func main() {
Usage: "Add the Password",
},
cli.StringFlag{
Name: "ctype, c", //Content Type Flag
Name: "ctype, c", // Content Type Flag
Usage: "Change the Content Type",
},
cli.StringFlag{
Expand All @@ -72,6 +76,10 @@ func main() {
Name: "editor, e",
Usage: "Open editor to insert request body",
},
cli.StringSliceFlag{
Name: "header, H",
Usage: "Header to pass with the request. Can be used multiple times.",
},
}
app.Commands = []cli.Command{
{
Expand Down
29 changes: 23 additions & 6 deletions methods/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"runtime"
"strings"

"github.com/fatih/color"
"github.com/urfave/cli"
Expand All @@ -20,9 +21,19 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
return "", err
}

var jsonStr []byte

if c.Bool("editor") {
// Check if we're being passed a request body from stdin.
// If so, use that. Otherwise, use the request data passed via cli flag.
var body []byte
stat, err := os.Stdin.Stat()
if err != nil {
return "", fmt.Errorf("error getting file info for stdin fd: %w", err)
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
body, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("error reading from stdin: %w", err)
}
} else if c.Bool("editor") {
var path string
var editor = os.Getenv("EDITOR")

Expand Down Expand Up @@ -69,15 +80,15 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
}

// read temp file contents
jsonStr, err = ioutil.ReadFile(tempFileName)
body, err = ioutil.ReadFile(tempFileName)
if err != nil {
return "", fmt.Errorf("Error reading file %s: %w", tempFileName, err)
}
} else {
jsonStr = []byte(c.String("body"))
body = []byte(c.String("body"))
}

req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonStr))
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return "", fmt.Errorf("Error creating request: %s", err.Error())
}
Expand All @@ -87,6 +98,12 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
}

for _, h := range c.StringSlice("header") {
kv := strings.Split(h, ": ")
req.Header.Add(kv[0], kv[1])
}

if c.String("u") != "" && c.String("p") != "" {
un := c.String("u")
pw := c.String("p")
Expand Down
8 changes: 7 additions & 1 deletion methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package methods
import (
"fmt"
"net/http"
"strings"

"github.com/urfave/cli"
)

//Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth
// Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth
func Getbasic(c *cli.Context) (string, error) {
var url, err = checkURL(c.Args().Get(0))
if err != nil {
Expand All @@ -29,6 +30,11 @@ func Getbasic(c *cli.Context) (string, error) {
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}

for _, h := range c.StringSlice("header") {
kv := strings.Split(h, ": ")
req.Header.Add(kv[0], kv[1])
}

client := getHTTPClient()
resp, err := client.Do(req)
if err != nil {
Expand Down

0 comments on commit e0c30de

Please sign in to comment.