forked from h2non/gentleman
/
composition.go
40 lines (31 loc) · 887 Bytes
/
composition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/mux"
"gopkg.in/h2non/gentleman.v2/plugins/url"
)
func main() {
// Create a new client
cli := gentleman.New()
// Define the server url (must be first)
cli.Use(url.URL("http://httpbin.org"))
// Create a new multiplexer based on multiple matchers
mx := mux.If(mux.Method("GET"), mux.Host("httpbin.org"))
// Attach a custom plugin on the multiplexer that will be executed if the matchers passes
mx.Use(url.Path("/headers"))
// Attach the multiplexer on the main client
cli.Use(mx)
// Perform the request
res, err := cli.Request().Send()
if err != nil {
fmt.Printf("Request error: %s\n", err)
return
}
if !res.Ok {
fmt.Printf("Invalid server response: %d\n", res.StatusCode)
return
}
fmt.Printf("Status: %d\n", res.StatusCode)
fmt.Printf("Body: %s", res.String())
}