six-ddc / plow

A high-performance HTTP benchmarking tool that includes a real-time web UI and terminal display
Apache License 2.0
4.12k stars 140 forks source link

[Question] Can I send cookies #32

Closed dodalovic closed 2 years ago

dodalovic commented 2 years ago

Hi, I really like the tool! I'm just not sure if there's a way to send cookies in the requests?

Thanks!

dodalovic commented 2 years ago

This works on my machine, for my purposes. Should I contribute?

Index: main.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/main.go b/main.go
--- a/main.go   (revision 514bef9850470396b5b9ff10d738352518bfc630)
+++ b/main.go   (date 1648726182395)
@@ -21,6 +21,7 @@
    stream      = kingpin.Flag("stream", "Specify whether to stream file specified by '--body @file' using chunked encoding or to read into memory").Default("false").Bool()
    method      = kingpin.Flag("method", "HTTP method").Default("GET").Short('m').String()
    headers     = kingpin.Flag("header", "Custom HTTP headers").Short('H').PlaceHolder("K:V").Strings()
+   cookies     = kingpin.Flag("cookies", "Custom HTTP cookies").PlaceHolder("K:V").Strings()
    host        = kingpin.Flag("host", "Host header").String()
    contentType = kingpin.Flag("content", "Content-Type header").Short('T').String()
    cert        = kingpin.Flag("cert", "Path to the client's TLS Certificate").ExistingFile()
@@ -142,6 +143,7 @@
        url:       *url,
        method:    *method,
        headers:   *headers,
+       cookie:    *cookies,
        bodyBytes: bodyBytes,
        bodyFile:  bodyFile,

Index: requester.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/requester.go b/requester.go
--- a/requester.go  (revision 514bef9850470396b5b9ff10d738352518bfc630)
+++ b/requester.go  (date 1648726189391)
@@ -93,6 +93,7 @@
    requests    int64
    duration    time.Duration
    clientOpt   *ClientOpt
+   cookie      *fasthttp.Cookie
    httpClient  *fasthttp.HostClient
    httpHeader  *fasthttp.RequestHeader

@@ -110,6 +111,7 @@
    url       string
    method    string
    headers   []string
+   cookie    []string
    bodyBytes []byte
    bodyFile  string

@@ -215,6 +217,12 @@
    } else {
        requestHeader.SetHost(u.Host)
    }
+   if len(opt.cookie) > 0 {
+       for _, value := range opt.cookie {
+           keyVal := strings.Split(value, ":")
+           requestHeader.SetCookie(keyVal[0], keyVal[1])
+       }
+   }
    requestHeader.SetMethod(opt.method)
    requestHeader.SetRequestURI(u.RequestURI())
    for _, h := range opt.headers {
six-ddc commented 2 years ago

Maybe you can directly use -H 'Cookie: K=V'

dodalovic commented 2 years ago

Yes, @six-ddc , that works wonderfully! Thanks!