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

Commit 40ae319

Browse files
authored
Merge pull request #43 from gbmor-forks/additional-headers
Include arbitrary headers with a request
2 parents 84d9335 + 094e333 commit 40ae319

4 files changed

Lines changed: 31 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ _(optional)_
123123
|`xml`|`application/xml`|
124124
|`plain`|`text/plain`|
125125

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`
130+
126131
### Providing a Request Body via stdin
127132

128133
In addition to `-b`/`--body`, you may provide a request body via stdin.

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
<<<<<<< HEAD
9+
"strings"
10+
=======
811
"os"
12+
>>>>>>> 84d9335cf8a8fede56f25b0c16981965393f5bc5
913

1014
"github.com/urfave/cli"
1115
)
@@ -43,6 +47,12 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
4347
var bearer = "Bearer " + c.String("token")
4448
req.Header.Add("Authorization", bearer)
4549
}
50+
51+
for _, h := range c.StringSlice("header") {
52+
kv := strings.Split(h, ": ")
53+
req.Header.Add(kv[0], kv[1])
54+
}
55+
4656
if c.String("u") != "" && c.String("p") != "" {
4757
un := c.String("u")
4858
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)