bodytype

package
v2.0.4+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2020 License: MIT Imports: 3 Imported by: 0

README

gentleman/bodytype Build Status GoDoc API Go Report Card

gentleman's plugin to easy define HTTP bodies. Supports JSON, XML, strings or streams with interface polymorphism.

Supported type aliases:

  • html - text/html
  • json - application/json
  • xml - application/xml
  • text - text/plain
  • urlencoded - application/x-www-form-urlencoded
  • form - application/x-www-form-urlencoded
  • form-data - application/x-www-form-urlencoded

Installation

go get -u gopkg.in/h2non/gentleman.v2/plugins/bodytype

API

See godoc reference.

Example

package main

import (
  "fmt"
  "gopkg.in/h2non/gentleman.v2"
  "gopkg.in/h2non/gentleman.v2/plugins/body"
  "gopkg.in/h2non/gentleman.v2/plugins/bodytype"
)

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

  // Define the JSON data to send 
  data := `{"foo":"bar"}`
  cli.Use(body.String(data))

  // We're sending a JSON based payload
  cli.Use(bodytype.Type("json"))

  // Perform the request
  res, err := cli.Request().Method("POST").URL("http://httpbin.org/post").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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Types = map[string]string{
	"html":       "text/html",
	"json":       "application/json",
	"xml":        "application/xml",
	"text":       "text/plain",
	"urlencoded": "application/x-www-form-urlencoded",
	"form":       "application/x-www-form-urlencoded",
	"form-data":  "application/x-www-form-urlencoded",
}

Types is a map of MIME type aliases

Functions

func Set

func Set(name string) p.Plugin

Set sets the Content Type header value, optionally based on a MIME type alias.

func Type

func Type(name string) p.Plugin

Type is an alias to Set, which defines the Content-Type header

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL