Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
add duration in curl
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Aug 31, 2018
1 parent a17b775 commit c2dae5a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions subcmd/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"regexp"
"strings"
"time"

"github.com/alecthomas/kingpin"
"github.com/codeskyblue/goreq"
Expand Down Expand Up @@ -68,12 +69,14 @@ var (
headers *http.Header
values *url.Values
bodyData string
duration time.Duration
)

func RegisterCurl(curl *kingpin.CmdClause) {
curl.Flag("request", "Specify request command to use").Short('X').Default("GET").StringVar(&method)
curl.Arg("url", "url string").Required().StringVar(&reqUrl)
curl.Flag("data", "body data").StringVar(&bodyData)
curl.Flag("timeout", "timeout send and receive response").Default("10s").DurationVar(&duration)
headers = HTTPHeader(curl.Flag("header", "Add a HTTP header to the request.").Short('H'))
values = HTTPValue(curl.Flag("form", "Add a HTTP form values").Short('F'))
}
Expand All @@ -87,6 +90,8 @@ func DoCurl() {
Uri: reqUrl,
}
request.ShowDebug = true
request.Timeout = duration

for k, values := range *headers {
for _, v := range values {
request.AddHeader(k, v)
Expand All @@ -95,6 +100,7 @@ func DoCurl() {
if method == "GET" {
request.QueryString = *values
} else if method == "POST" {
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
if bodyData != "" {
request.Body = bodyData
} else {
Expand Down

0 comments on commit c2dae5a

Please sign in to comment.