Skip to content

Commit

Permalink
fix: avoid https request error
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu Xuan committed Jun 22, 2021
1 parent 6209286 commit 160da74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions http2curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func GetCurlCommand(req *http.Request) (*CurlCommand, error) {

command.append("curl")

if req.URL != nil && req.URL.Scheme == "https" {
command.append("-k")
}

command.append("-X", bashEscape(req.Method))

if req.Body != nil {
Expand Down
19 changes: 19 additions & 0 deletions http2curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ func ExampleGetCurlCommand_other() {
// Output: curl -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
}

func ExampleGetCurlCommand_https() {
uri := "https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu"
payload := new(bytes.Buffer)
payload.Write([]byte(`{"hello":"world","answer":42}`))
req, err := http.NewRequest("PUT", uri, payload)
if err != nil {
panic(err)
}
req.Header.Set("X-Auth-Token", "private-token")
req.Header.Set("Content-Type", "application/json")

command, err := GetCurlCommand(req)
if err != nil {
panic(err)
}
fmt.Println(command)
// Output: curl -k -X 'PUT' -d '{"hello":"world","answer":42}' -H 'Content-Type: application/json' -H 'X-Auth-Token: private-token' 'https://www.example.com/abc/def.ghi?jlk=mno&pqr=stu'
}

// Benchmark test for GetCurlCommand
func BenchmarkGetCurlCommand(b *testing.B) {
form := url.Values{}
Expand Down

0 comments on commit 160da74

Please sign in to comment.