Add support for `--data-urlencode`, and for multiple data options

This commit attempts to bring greater consistency between
cURL, and wuzz use of body data.

Previously, when `--data` was used more than once, only the last
use of it would be added to the request body. cURL however, allows
multiple 'data' options to be specified, and it will simply
merge them using the `&` symbol separator.

Furthermore, this commit removes the default query unescaping for
body data so that `--data-urlencode` can be supported. This is to
emulate the behaviour in cURL where `--data-urlencode` will use
percent encoding on the given data. If the default query unescaping
was not removed, we will always show, and send unescaped data.

Example:

	wuzz -k curl -X POST http://kong:8001/apis/123-456-789/plugins \
		--data "name=key-auth" \
		--data "config.hide_credentials=true"

In the future, `@` support should be added for data. That is, it
should be able to load a file to include as part of the payload.

Also, for percent encoding to work, this commit takes the argument
data, and constructs a Go `url.URL` struct. It then converts that
URL into a string to take advantage of its internal logic for encoding
spaces. Once `wuzz` is upgraded to build using Go 1.8, we can then
use `PathEscape` instead to achieve this.
pull/94/head
Ian Lai 7 years ago
parent c8a6ff4569
commit 3ad407e0a1

@ -1359,6 +1359,7 @@ func (a *App) ParseArgs(g *gocui.Gui, args []string) error {
arg_index := 1
args_len := len(args)
accept_types := make([]string, 0, 8)
var body_data []string
for arg_index < args_len {
arg := args[arg_index]
switch arg {
@ -1369,7 +1370,7 @@ func (a *App) ParseArgs(g *gocui.Gui, args []string) error {
arg_index += 1
header := args[arg_index]
fmt.Fprintf(vheader, "%v\n", header)
case "-d", "--data", "--data-binary":
case "-d", "--data", "--data-binary", "--data-urlencode":
if arg_index == args_len-1 {
return errors.New("No POST/PUT/PATCH value specified")
}
@ -1377,14 +1378,19 @@ func (a *App) ParseArgs(g *gocui.Gui, args []string) error {
arg_index += 1
set_data = true
set_binary_data = arg == "--data-binary"
arg_data := args[arg_index]
data := args[arg_index]
if !set_binary_data {
data, _ = url.QueryUnescape(data)
content_type = "form"
}
vdata, _ := g.View(REQUEST_DATA_VIEW)
setViewTextAndCursor(vdata, data)
if arg == "--data-urlencode" {
// TODO: Replace with `url.PathEscape(..)` in Go 1.8
arg_data_url := &url.URL{Path: arg_data}
arg_data = arg_data_url.String()
}
body_data = append(body_data, arg_data)
case "-j", "--json":
if arg_index == args_len-1 {
return errors.New("No POST/PUT/PATCH value specified")
@ -1581,6 +1587,14 @@ func (a *App) ParseArgs(g *gocui.Gui, args []string) error {
fmt.Fprintf(vheader, "Accept: %v\n", strings.Join(accept_types, ","))
}
var merged_body_data string
if set_data && !set_binary_data {
merged_body_data = strings.Join(body_data, "&")
}
vdata, _ := g.View(REQUEST_DATA_VIEW)
setViewTextAndCursor(vdata, merged_body_data)
return nil
}

Loading…
Cancel
Save