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

Commit

Permalink
Merge pull request #43 from gbmor-forks/additional-headers
Browse files Browse the repository at this point in the history
Include arbitrary headers with a request
  • Loading branch information
athul authored Dec 8, 2021
2 parents 84d9335 + 094e333 commit 40ae319
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ _(optional)_
|`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.
Expand Down
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,13 +65,17 @@ 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{
Name: "body, b",
Usage: "Body of the Post Request",
},
cli.StringSliceFlag{
Name: "header, H",
Usage: "Header to pass with the request. Can be used multiple times.",
},
}
app.Commands = []cli.Command{
{
Expand Down
10 changes: 10 additions & 0 deletions methods/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"fmt"
"io"
"net/http"
<<<<<<< HEAD
"strings"
=======
"os"
>>>>>>> 84d9335cf8a8fede56f25b0c16981965393f5bc5

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -43,6 +47,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 40ae319

Please sign in to comment.