forked from h2non/gentleman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
39 lines (33 loc) · 851 Bytes
/
plugin.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
package main
import (
"fmt"
"gopkg.in/h2non/gentleman.v1"
"gopkg.in/h2non/gentleman.v1/context"
"gopkg.in/h2non/gentleman.v1/plugin"
"gopkg.in/h2non/gentleman.v1/plugins/headers"
"net/url"
)
func main() {
// Create a new client
cli := gentleman.New()
// Define a custom header
cli.Use(headers.Set("Token", "s3cr3t"))
// Create a request plugin to define the URL
cli.Use(plugin.NewRequestPlugin(func(ctx *context.Context, h context.Handler) {
u, _ := url.Parse("http://httpbin.org/headers")
ctx.Request.URL = u
h.Next(ctx)
}))
// 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())
}