You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/vendor/gopkg.in/h2non/gentleman.v2/plugin/README.md

1.8 KiB

gentleman/plugin Build Status GoDoc API Go Report Card

plugin package implements a simple middleware-based plugin layer especially designed for HTTP clients and complete HTTP request/response live cycle control.

The package exposes a simple API with multiple factory helper functions to create plugins more easily.

Installation

go get -u gopkg.in/h2non/gentleman.v2/plugin

API

See godoc reference.

Examples

Create a request plugin

package main

import (
  "fmt"
  "gopkg.in/h2non/gentleman.v2"
  "gopkg.in/h2non/gentleman.v2/context"
  "gopkg.in/h2non/gentleman.v2/plugin"
  "net/url"
)

func main() {
  // Create a new client
  cli := gentleman.New()

  // 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())
}

License

MIT - Tomas Aparicio