Skip to content

Commit

Permalink
Add support of reading data from the file using @
Browse files Browse the repository at this point in the history
  • Loading branch information
legal90 committed Jul 8, 2020
1 parent 522f0d6 commit 3c67a45
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ $ awscurl --service execute-api \
-H "Content-Type: application/json" \
"https://<prefix>.execute-api.us-east-1.amazonaws.com/<resource>"
```
or reading data from the file:
```
$ awscurl --service execute-api \
-X POST \
-d @./path/to/file.json \
-H "Content-Type: application/json" \
"https://<prefix>.execute-api.us-east-1.amazonaws.com/<resource>"
```

## Related projects

Expand Down
19 changes: 16 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -60,9 +61,9 @@ func main() {

func init() {
rootCmd.PersistentFlags().StringVarP(&flags.method, "request", "X", "GET", "Custom request method to use")
rootCmd.PersistentFlags().StringVarP(&flags.data, "data", "d", "", "Data payload to send within a POST request")
rootCmd.PersistentFlags().StringVarP(&flags.data, "data", "d", "", `Data payload to send within a request. Could be also read from a file if prefixed with @, example: -d "@/path/to/file.json"`)
rootCmd.PersistentFlags().StringArrayVarP(&flags.headers, "header", "H", []string{},
`Extra HTTP header to include in the request. Example: "Content-Type: application/json". Could be used multiple times`)
`Extra HTTP header to include in the request. Example: -h "Content-Type: application/json". Could be used multiple times`)
rootCmd.PersistentFlags().StringVar(&flags.awsAccessKey, "access-key", "", "AWS Access Key ID to use for authentication")
rootCmd.PersistentFlags().StringVar(&flags.awsSecretKey, "secret-key", "", "AWS Secret Access Key to use for authentication")
rootCmd.PersistentFlags().StringVar(&flags.awsSessionToken, "session-token", "", "AWS Session Key to use for authentication")
Expand All @@ -87,9 +88,21 @@ func runCurl(cmd *cobra.Command, args []string) error {
return err
}

var body io.Reader

if strings.HasPrefix(flags.data, "@") {
// Read data from file
fPath := flags.data[1:]
body, err = os.Open(fPath)
if err != nil {
return err
}
} else {
body = strings.NewReader(flags.data)
}

// Build the HTTP request
url := args[0]
body := strings.NewReader(flags.data)
req, err := http.NewRequest(flags.method, url, body)
if err != nil {
return err
Expand Down

0 comments on commit 3c67a45

Please sign in to comment.