Skip to content

Commit 943e649

Browse files
committed
swap what was just content-type for headers
- om curl now has the abilty to specify headers instead of just Content-Type
1 parent f1d07c7 commit 943e649

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

api/request_service.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
type RequestServiceInvokeInput struct {
10-
Path string
11-
Method string
12-
Data io.Reader
13-
ContentType string
10+
Path string
11+
Method string
12+
Data io.Reader
13+
Headers http.Header
1414
}
1515

1616
type RequestServiceInvokeOutput struct {
@@ -33,7 +33,7 @@ func (rs RequestService) Invoke(input RequestServiceInvokeInput) (RequestService
3333
return RequestServiceInvokeOutput{}, fmt.Errorf("failed constructing request: %s", err)
3434
}
3535

36-
request.Header.Set("Content-Type", input.ContentType)
36+
request.Header = input.Headers
3737
response, err := rs.client.Do(request)
3838
if err != nil {
3939
return RequestServiceInvokeOutput{}, fmt.Errorf("failed submitting request: %s", err)

api/request_service_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ var _ = Describe("RequestService", func() {
3535
}, nil)
3636

3737
output, err := service.Invoke(api.RequestServiceInvokeInput{
38-
Method: "PUT",
39-
Path: "/api/v0/api/endpoint",
40-
Data: strings.NewReader("some-request-body"),
41-
ContentType: "application/json",
38+
Method: "PUT",
39+
Path: "/api/v0/api/endpoint",
40+
Data: strings.NewReader("some-request-body"),
41+
Headers: http.Header{"Content-Type": []string{"application/json"}},
4242
})
4343
Expect(err).NotTo(HaveOccurred())
4444

commands/curl.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ type Curl struct {
2424
stdout logger
2525
stderr logger
2626
Options struct {
27-
Path string `short:"p" long:"path" description:"path to api endpoint"`
28-
Method string `short:"x" long:"request" description:"http verb" default:"GET"`
29-
Data string `short:"d" long:"data" description:"api request payload"`
30-
Silent bool `short:"s" long:"silent" description:"only write response headers to stderr if response status is 4XX or 5XX"`
31-
ContentType string `short:"c" long:"content-type" description:"content type of payload" default:"application/json"`
27+
Path string `short:"p" long:"path" description:"path to api endpoint"`
28+
Method string `short:"x" long:"request" description:"http verb" default:"GET"`
29+
Data string `short:"d" long:"data" description:"api request payload"`
30+
Silent bool `short:"s" long:"silent" description:"only write response headers to stderr if response status is 4XX or 5XX"`
31+
Headers flags.StringSlice `short:"H" long:"header" description:"used to specify custom headers with your command" default:"Content-Type: application/json"`
3232
}
3333
}
3434

@@ -46,11 +46,18 @@ func (c Curl) Execute(args []string) error {
4646
return errors.New("could not parse curl flags: -path is a required parameter. Please run `om curl --help` for more info.")
4747
}
4848

49+
requestHeaders := make(http.Header)
50+
51+
for _, h := range c.Options.Headers {
52+
split := strings.Split(h, " ")
53+
requestHeaders.Set(strings.TrimSuffix(split[0], ":"), split[1])
54+
}
55+
4956
input := api.RequestServiceInvokeInput{
50-
Path: c.Options.Path,
51-
Method: c.Options.Method,
52-
Data: strings.NewReader(c.Options.Data),
53-
ContentType: c.Options.ContentType,
57+
Path: c.Options.Path,
58+
Method: c.Options.Method,
59+
Data: strings.NewReader(c.Options.Data),
60+
Headers: requestHeaders,
5461
}
5562

5663
output, err := c.requestService.Invoke(input)

commands/curl_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var _ = Describe("Curl", func() {
5858
input := requestService.InvokeArgsForCall(0)
5959
Expect(input.Path).To(Equal("/api/v0/some/path"))
6060
Expect(input.Method).To(Equal("POST"))
61-
Expect(input.ContentType).To(Equal("application/json"))
61+
Expect(input.Headers).To(HaveKeyWithValue("Content-Type", []string{"application/json"}))
6262

6363
data, err := ioutil.ReadAll(input.Data)
6464
Expect(err).NotTo(HaveOccurred())
@@ -152,14 +152,14 @@ var _ = Describe("Curl", func() {
152152
"--path", "/api/v0/some/path",
153153
"--request", "POST",
154154
"--data", `some_key=some_value`,
155-
"--content-type", "application/x-www-form-urlencoded",
155+
"--header", "Content-Type: application/x-www-form-urlencoded",
156156
})
157157
Expect(err).NotTo(HaveOccurred())
158158

159159
input := requestService.InvokeArgsForCall(0)
160160
Expect(input.Path).To(Equal("/api/v0/some/path"))
161161
Expect(input.Method).To(Equal("POST"))
162-
Expect(input.ContentType).To(Equal("application/x-www-form-urlencoded"))
162+
Expect(input.Headers).To(HaveKeyWithValue("Content-Type", []string{"application/x-www-form-urlencoded"}))
163163
})
164164
})
165165

0 commit comments

Comments
 (0)