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

Commit 6ca1516

Browse files
committed
include arbitrary headers with a request
1 parent 9209801 commit 6ca1516

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,8 @@ _(optional)_
122122
|`html`|`text/html`|
123123
|`xml`|`application/xml`|
124124
|`plain`|`text/plain`|
125+
126+
### Include Arbitrary Headers
127+
- `-H` or `--header` may be specified multiple times to include headers with the request.
128+
- Example:
129+
- `hopp-cli get -H 'X-Api-Key: foobar' -H 'X-Api-Secret: super_secret' https://example.com/api/v1/accounts`

cli.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func main() {
3535
Name: "p",
3636
Usage: "Add the Password",
3737
},
38+
cli.StringSliceFlag{
39+
Name: "header, H",
40+
Usage: "Header to pass with the request. Can be used multiple times.",
41+
},
3842
}
3943
genFlags := []cli.Flag{
4044
cli.IntFlag{
@@ -61,13 +65,17 @@ func main() {
6165
Usage: "Add the Password",
6266
},
6367
cli.StringFlag{
64-
Name: "ctype, c", //Content Type Flag
68+
Name: "ctype, c", // Content Type Flag
6569
Usage: "Change the Content Type",
6670
},
6771
cli.StringFlag{
6872
Name: "body, b",
6973
Usage: "Body of the Post Request",
7074
},
75+
cli.StringSliceFlag{
76+
Name: "header, H",
77+
Usage: "Header to pass with the request. Can be used multiple times.",
78+
},
7179
}
7280
app.Commands = []cli.Command{
7381
{

methods/basic.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"net/http"
7+
"strings"
78

89
"github.com/urfave/cli"
910
)
@@ -26,6 +27,12 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
2627
var bearer = "Bearer " + c.String("token")
2728
req.Header.Add("Authorization", bearer)
2829
}
30+
31+
for _, h := range c.StringSlice("header") {
32+
kv := strings.Split(h, ": ")
33+
req.Header.Add(kv[0], kv[1])
34+
}
35+
2936
if c.String("u") != "" && c.String("p") != "" {
3037
un := c.String("u")
3138
pw := c.String("p")

methods/get.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package methods
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67

78
"github.com/urfave/cli"
89
)
910

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

33+
for _, h := range c.StringSlice("header") {
34+
kv := strings.Split(h, ": ")
35+
req.Header.Add(kv[0], kv[1])
36+
}
37+
3238
client := getHTTPClient()
3339
resp, err := client.Do(req)
3440
if err != nil {

0 commit comments

Comments
 (0)