Skip to content

Commit

Permalink
internal/client: add initial body data
Browse files Browse the repository at this point in the history
  • Loading branch information
igarciaolaizola committed Sep 7, 2018
1 parent 3bda791 commit bb6a25d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ go run cmd/h2-stream/main.go serve --addr=localhost:8080 --tls=false

launch client:
```
go run cmd/h2-stream/main.go cli --addr=http://localhost:8080 --method=POST
go run cmd/h2-stream/main.go cli --addr=http://localhost:8080 --method=POST --data="BODY DATA"
```

### with TLS
Expand All @@ -29,5 +29,5 @@ go run cmd/h2-stream/main.go serve --addr=localhost:8080 --tls=true --cert=certs

launch client:
```
go run cmd/h2-stream/main.go cli --addr=https://localhost:8080 --method=POST --insecure
go run cmd/h2-stream/main.go cli --addr=https://localhost:8080 --method=POST --data="BODY DATA" --insecure
```
1 change: 1 addition & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func newClientCommand() *cobra.Command {
cmd.Flags().StringVar(&cfg.Addr, "addr", "https://localhost:8080", "http server address")
cmd.Flags().StringVar(&cfg.Method, "method", "POST", "http method (GET, POST, PUT, DELETE...)")
cmd.Flags().BoolVar(&cfg.Insecure, "insecure", false, "skip tls validation")
cmd.Flags().StringVar(&cfg.Data, "data", "", "initial body data to be sent")
cmd.Flags().StringArrayVar(&cfg.Headers, "header", nil, "custom headers, ex.: \"Content-Type: application/json\"")
return cmd
}
11 changes: 10 additions & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
Method string
Insecure bool
Headers []string
Data string
}

// Client is a http2 client
Expand Down Expand Up @@ -65,8 +66,16 @@ func New(cfg Config) (*Client, error) {

// Run launches a client
func (c *Client) Run() error {
// Set body reader
var body io.Reader
if c.Data != "" {
body = io.MultiReader(strings.NewReader(c.Data), os.Stdin)
} else {
body = os.Stdin
}

// Establish http connection
req, err := http.NewRequest(c.Method, c.Addr, os.Stdin)
req, err := http.NewRequest(c.Method, c.Addr, body)
if c.Headers != nil {
for _, h := range c.Headers {
kv := strings.Split(h, ":")
Expand Down

0 comments on commit bb6a25d

Please sign in to comment.