Skip to content

Commit 3e59300

Browse files
committed
Examples: Update the "valyala/fasthttp" example to send HTTP body as POST
Related: #112 (cherry picked from commit ca6559d)
1 parent 679688b commit 3e59300

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

_examples/fasthttp/cmd/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package main
66

77
import (
8+
"context"
89
"log"
910
"os"
1011
"sort"
1112
"strconv"
13+
"strings"
1214
"time"
1315

1416
"github.com/elastic/go-elasticsearch/v7"
@@ -42,6 +44,31 @@ func main() {
4244
log.Fatalf("Error creating the client: %s", err)
4345
}
4446

47+
// Test sending the body as POST
48+
//
49+
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
50+
defer cancel()
51+
52+
go func() {
53+
es.Info()
54+
if _, err := es.Search(
55+
es.Search.WithBody(strings.NewReader(`{"query":{"match":{"title":"foo"}}}`)),
56+
es.Search.WithPretty(),
57+
); err != nil {
58+
log.Fatalf("Error getting response: %s", err)
59+
}
60+
cancel()
61+
}()
62+
63+
select {
64+
case <-ctx.Done():
65+
if ctx.Err() != context.Canceled {
66+
log.Fatalf("Timeout: %s", ctx.Err())
67+
}
68+
}
69+
70+
// Run benchmark
71+
//
4572
t := time.Now()
4673
for i := 0; i < count; i++ {
4774
t0 := time.Now()

_examples/fasthttp/fasthttp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
4242
// copyRequest converts a http.Request to fasthttp.Request
4343
//
4444
func (t *Transport) copyRequest(dst *fasthttp.Request, src *http.Request) *fasthttp.Request {
45+
if src.Method == "GET" && src.Body != nil {
46+
src.Method = "POST"
47+
}
48+
4549
dst.SetHost(src.Host)
4650
dst.SetRequestURI(src.URL.String())
4751

_examples/fasthttp/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ go 1.11
55
replace github.com/elastic/go-elasticsearch/v7 => ../..
66

77
require (
8-
github.com/elastic/go-elasticsearch/v7 v7.0.0-20190407092644-3fb2a278216b
8+
github.com/elastic/go-elasticsearch/v7 7.x
99
github.com/valyala/fasthttp v1.5.0
1010
)

0 commit comments

Comments
 (0)