request

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 6 Imported by: 35

README

Request Mentioned in Awesome Go Go Report Card Go

GoDoc codecov Release TODOs License

HTTP client for golang, Inspired by Javascript-axios Python-request. If you have experience about axios or requests, you will love it. No 3rd dependency.

Features

  • Make http requests from Golang
  • Transform request and response data

Installing

go mod:

go get github.com/monaco-io/request

Methods

  • OPTIONS
  • GET
  • HEAD
  • POST
  • PUT
  • DELETE
  • TRACE
  • CONNECT

Example

POST
package main

import (
    "github.com/monaco-io/request"
)

func main() {
    client := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Query: map[string]string{"hello": "world"},
        JSON:   []byte(`{"hello": "world"}`),
    }
    var result interface{}
    resp := client.Send()
    err := resp.Scan(&result).Error()
    str := resp.String()
    bytes := resp.Bytes()
    ...
POST with empty request
package main

import (
    "github.com/monaco-io/request"
)

func main() {
    var data interface{}

    resp := request.
        New().
        POST("http://httpbin.org/post").
        AddHeader(map[string]string{"Google": "google"}).
        AddBasicAuth("google", "google").
        AddURLEncodedForm(map[string]string{"data": "google"}).
        Send().
        Scan(&data)
    ...
Authorization
package main

import (
    "github.com/monaco-io/request"
)

func main() {
    client := request.Client{
        URL:       "https://google.com",
        Method:    "POST",
        BasicAuth: request.BasicAuth{
            Username:"user_xxx",
            Password:"pwd_xxx",
        },
    }
}
Timeout
package main

import (
    "github.com/monaco-io/request"
)

func main() {
    client := request.Client{
        URL:       "https://google.com",
        Method:    "POST",
        Timeout:   time.Second*10,
    }
}
Cookies
package main

import (
    "github.com/monaco-io/request"
)

func main() {
    client := request.Client{
        URL:       "https://google.com",
        CookiesMap: map[string]string{
            "cookie_name": "cookie_value",
        }
    }
}
TLS
package main

import (

    "github.com/monaco-io/request"
)

func main() {
    client := request.Client{
        URL:       "https://google.com",
        TLSConfig: &tls.Config{InsecureSkipVerify: true},
    }
}

License

MIT

Documentation

Overview

Package request HTTP client for golang

  • Make http requests from Golang
  • Intercept request and response
  • Transform request and response data

GET

client := request.Client{
    URL:    "https://google.com",
    Method: "GET",
    Query: map[string]string{"hello": "world"},
}
resp := client.Send()

POST

client := request.Client{
    URL:    "https://google.com",
    Method: "POST",
    Query: map[string]string{"hello": "world"},
    JSON:   []byte(`{"hello": "world"}`),
}
resp := client.Send()

Content-Type

client := request.Client{
    URL:          "https://google.com",
    Method:       "POST",
    ContentType: request.ApplicationXWwwFormURLEncoded, // default is "application/json"
}
resp := client.Send()

Authorization

client := request.Client{
    URL:       "https://google.com",
    Method:    "POST",
    BasicAuth:      request.BasicAuth{
        Username:"user_xxx",
        Password:"pwd_xxx",
    }, // xxx:xxx
}

resp := client.Send()

Cookies

client := request.Client{
    URL:       "https://google.com",
    Cookies:[]*http.Cookie{
         {
          Name:  "cookie_name",
          Value: "cookie_value",
         },
    },
}

resp := client.Send()

Index

Constants

View Source
const (
	// OPTIONS http options
	OPTIONS = "OPTIONS"

	// GET http get
	GET = "GET"

	// HEAD http head
	HEAD = "HEAD"

	// POST http post
	POST = "POST"

	// PUT http put
	PUT = "PUT"

	// DELETE http delete
	DELETE = "DELETE"

	// TRACE http trace
	TRACE = "TRACE"

	// CONNECT http connect
	CONNECT = "CONNECT"

	// PATCH http patch
	PATCH = "PATCH"
)
View Source
const Version = "v1.0.9"

Version of module github.com/monaco-io/request

Variables

This section is empty.

Functions

func New added in v1.0.8

func New() *request.Request

New a empty request

Types

type A added in v1.0.8

type A []interface{}

A alias of []interface{}

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

BasicAuth Add Username:Password as Basic Auth

type Client

type Client struct {
	// URL http request url like: https://www.google.com
	URL string

	// Method http method GET/POST/POST/DELETE ...
	Method string

	// Header http header
	Header map[string]string

	// SortedHeader http sorted header, example: [][2]string{{"h1": "v1"}, {"h2": "v2"}}
	SortedHeader [][2]string

	// Query params on http url
	Query map[string]string

	// JSON body as json string/bytes/struct
	JSON interface{}

	// XML body as xml string/bytes/struct
	XML interface{}

	// YAML body as yaml string/bytes/struct
	YAML interface{}

	// XML body as string
	String string

	// URLEncodedForm string/bytes/map[string][]string
	URLEncodedForm interface{}

	// FormFields TODO
	FormFields map[string]string

	// BasicAuth http basic auth with username and password
	BasicAuth BasicAuth

	// CustomerAuth add Authorization xxx to header
	CustomerAuth string

	// CustomerAuth add Authorization bearer xxx to header
	Bearer string

	// Timeout http request timeout
	Timeout time.Duration

	// TLSTimeout tls timeout
	TLSTimeout time.Duration

	// DialTimeout dial timeout
	DialTimeout time.Duration

	// ProxyURL proxy url
	ProxyURL string

	// Define the proxy function to be used during the transport
	ProxyServers map[string]string

	// Cookies original http cookies
	Cookies []*http.Cookie

	// CookiesMap add cookies as map
	CookiesMap map[string]string

	// TLSConfig tls config on transport
	TLSConfig *tls.Config

	// Transport http transport
	Transport *http.Transport
}

Client Method

  Method         = "OPTIONS"                ; Section 9.2
                 | "GET"                    ; Section 9.3
                 | "HEAD"                   ; Section 9.4
                 | "POST"                   ; Section 9.5
                 | "PUT"                    ; Section 9.6
                 | "DELETE"                 ; Section 9.7
                 | "TRACE"                  ; Section 9.8
                 | "CONNECT"                ; Section 9.9
                 | extension-method
extension-method = token
  token          = 1*<any CHAR except CTLs or separators>

func (*Client) Send added in v1.0.6

func (c *Client) Send() *response.Sugar

Send http request

type H added in v1.0.8

type H map[string]interface{}

H alias of map[string]interface{}

Directories

Path Synopsis
header command

Jump to

Keyboard shortcuts

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