Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 48 additions & 39 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@ type App struct {

Cfg config.Config

AWSSigv4 *aws.Config
Basic *core.KeyVal
Bearer string
BuildInfo bool
Complete string
ConfigPath string
Data io.Reader
DryRun bool
Edit bool
Form []core.KeyVal
Help bool
JSON io.Reader
Method string
Multipart []core.KeyVal
Output string
OutputDir bool
Range []string
UnixSocket string
Update bool
Version bool
XML io.Reader
AWSSigv4 *aws.Config
Basic *core.KeyVal
Bearer string
BuildInfo bool
Complete string
ConfigPath string
ContentType string
Data io.Reader
DryRun bool
Edit bool
Form []core.KeyVal
Help bool
Method string
Multipart []core.KeyVal
Output string
OutputDir bool
Range []string
UnixSocket string
Update bool
Version bool

dataSet, jsonSet, xmlSet bool
}

func (a *App) PrintHelp(p *core.Printer) {
Expand Down Expand Up @@ -277,14 +278,18 @@ func (a *App) CLI() *CLI {
Description: "Send a request body",
Default: "",
IsSet: func() bool {
return a.Data != nil
return a.dataSet
},
Fn: func(value string) error {
r, err := requestBody(value)
r, path, err := requestBody(value)
if err != nil {
return err
}
a.Data = r
a.Data, a.ContentType, err = core.DetectContentType(r, path)
if err != nil {
return err
}
a.dataSet = true
return nil
},
},
Expand Down Expand Up @@ -489,14 +494,16 @@ func (a *App) CLI() *CLI {
Description: "Send a JSON request body",
Default: "",
IsSet: func() bool {
return a.JSON != nil
return a.jsonSet
},
Fn: func(value string) error {
r, err := requestBody(value)
r, _, err := requestBody(value)
if err != nil {
return err
}
a.JSON = r
a.Data = r
a.ContentType = "application/json"
a.jsonSet = true
return nil
},
},
Expand Down Expand Up @@ -810,52 +817,54 @@ func (a *App) CLI() *CLI {
Description: "Send an XML request body",
Default: "",
IsSet: func() bool {
return a.XML != nil
return a.xmlSet
},
Fn: func(value string) error {
r, err := requestBody(value)
r, _, err := requestBody(value)
if err != nil {
return err
}
a.XML = r
a.Data = r
a.ContentType = "application/xml"
a.xmlSet = true
return nil
},
},
},
}
}

func requestBody(value string) (io.Reader, error) {
func requestBody(value string) (io.Reader, string, error) {
switch {
case len(value) == 0 || value[0] != '@':
return strings.NewReader(value), nil
return strings.NewReader(value), "", nil
case value == "@-":
return os.Stdin, nil
return os.Stdin, "", nil
default:
path := value[1:]
// Expand '~' to the home directory.
if len(path) >= 2 && path[0] == '~' && path[1] == os.PathSeparator {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
return nil, "", err
}
path = home + path[1:]
}
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, core.FileNotExistsError(value[1:])
return nil, "", core.FileNotExistsError(value[1:])
}
return nil, err
return nil, "", err
}
info, err := f.Stat()
if err != nil {
return nil, err
return nil, "", err
}
if info.IsDir() {
return nil, fileIsDirError(value[1:])
return nil, "", fileIsDirError(value[1:])
}
return f, nil
return f, path, nil
}
}

Expand Down
13 changes: 3 additions & 10 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,17 @@ type RequestConfig struct {
AWSSigV4 *aws.Config
Basic *core.KeyVal
Bearer string
ContentType string
Data io.Reader
Form []core.KeyVal
Headers []core.KeyVal
HTTP core.HTTPVersion
JSON io.Reader
Method string
Multipart *multipart.Multipart
NoEncode bool
QueryParams []core.KeyVal
Range []string
URL *url.URL
XML io.Reader
}

// NewRequest returns an *http.Request given the provided configuration.
Expand All @@ -279,12 +278,8 @@ func (c *Client) NewRequest(ctx context.Context, cfg RequestConfig) (*http.Reque
q.Add(f.Key, f.Val)
}
body = strings.NewReader(q.Encode())
case cfg.JSON != nil:
body = cfg.JSON
case cfg.Multipart != nil:
body = cfg.Multipart
case cfg.XML != nil:
body = cfg.XML
}

// If no scheme was provided, use various heuristics to choose between
Expand Down Expand Up @@ -319,10 +314,8 @@ func (c *Client) NewRequest(ctx context.Context, cfg RequestConfig) (*http.Reque
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
case cfg.Multipart != nil:
req.Header.Set("Content-Type", cfg.Multipart.ContentType())
case cfg.JSON != nil:
req.Header.Set("Content-Type", "application/json")
case cfg.XML != nil:
req.Header.Set("Content-Type", "application/xml")
case cfg.ContentType != "":
req.Header.Set("Content-Type", cfg.ContentType)
}

// Optionally set the range header.
Expand Down
Loading