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

Commit 094e333

Browse files
committed
Merge branch 'master' of https://github.com/hoppscotch/hopp-cli into additional-headers
2 parents 6ca1516 + 84d9335 commit 094e333

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,22 @@ _(optional)_
127127
- `-H` or `--header` may be specified multiple times to include headers with the request.
128128
- Example:
129129
- `hopp-cli get -H 'X-Api-Key: foobar' -H 'X-Api-Secret: super_secret' https://example.com/api/v1/accounts`
130+
131+
### Providing a Request Body via stdin
132+
133+
In addition to `-b`/`--body`, you may provide a request body via stdin.
134+
If you combine this method with the `-b` flag, the body provided with `-b` will be ignored.
135+
136+
**Example with Pipes**
137+
```shell
138+
$ echo '{"foo":"bar"}' | hopp-cli post -c js http://example.com
139+
```
140+
**Example with Redirection**
141+
```shell
142+
$ cat myrequest.json
143+
{
144+
"foo": "bar"
145+
}
146+
147+
$ hopp-cli post -c js http://example.com <myrequest.json
148+
```

methods/basic.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package methods
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"net/http"
8+
<<<<<<< HEAD
79
"strings"
10+
=======
11+
"os"
12+
>>>>>>> 84d9335cf8a8fede56f25b0c16981965393f5bc5
813

914
"github.com/urfave/cli"
1015
)
@@ -16,8 +21,23 @@ func BasicRequestWithBody(c *cli.Context, method string) (string, error) {
1621
return "", err
1722
}
1823

19-
var jsonStr = []byte(c.String("body"))
20-
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonStr))
24+
// Check if we're being passed a request body from stdin.
25+
// If so, use that. Otherwise, use the request data passed via cli flag.
26+
var body []byte
27+
stat, err := os.Stdin.Stat()
28+
if err != nil {
29+
return "", fmt.Errorf("error getting file info for stdin fd: %w", err)
30+
}
31+
if (stat.Mode() & os.ModeCharDevice) == 0 {
32+
body, err = io.ReadAll(os.Stdin)
33+
if err != nil {
34+
return "", fmt.Errorf("error reading from stdin: %w", err)
35+
}
36+
} else {
37+
body = []byte(c.String("body"))
38+
}
39+
40+
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
2141
if err != nil {
2242
return "", fmt.Errorf("Error creating request: %s", err.Error())
2343
}

0 commit comments

Comments
 (0)