request

package module
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: MIT Imports: 9 Imported by: 18

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() {
    var body = struct {
         A string
         B int
        }{A: "A", B: 1}
    var result interface{}

    c := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Query: map[string]string{"hello": "world"},
        JSON:   body,
    }
    resp := c.Send().Scan(&result)
    if !resp.OK(){
        // handle error
        log.Println(resp.Error())
    }

    // str := resp.String()
    // bytes := resp.Bytes()
POST with local files
package main

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

func main() {
    c := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Query: map[string]string{"hello": "world"},
        MultipartForm: MultipartForm{
            Fields: map[string]string{"a": "1"},
			Files:  []string{"doc.txt"},
        },
    }
    resp := c.Send().Scan(&result)
    ...
POST step by step
package main

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

func main() {
    var response 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(&response)
    ...
POST with context (1/2)
package main

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

func main() {
    c := request.Client{
        Context: context.Background(),
        URL:       "https://google.com",
        Method:    "POST",
        BasicAuth: request.BasicAuth{
            Username: "google",
            Password: "google",
        },
    }
    resp := c.Send()
    ...
POST with context (2/2)
package main

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

func main() {
    var response interface{}

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

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

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

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

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

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

func main() {
    c := 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() {
    c := 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

package main

import (

"github.com/monaco-io/request"

)

func main() {
    var body = struct {
         A string
         B int
        }{A: "A", B: 001}
    var result interface{}

    client := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Query: map[string]string{"hello": "world"},
        JSON:   body,
    }
    if err := client.Send().Scan(&result).Error(); err != nil{
        // handle error
    }

    // str := client.Send().String()
    // bytes := client.Send().Bytes()

```

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.16"

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

func NewWithContext added in v1.0.13

func NewWithContext(ctx originContext.Context) *request.Request

NewWithContext 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 {
	// Context go context
	Context context.Context

	// 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{}

	// String body as string
	String string

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

	// MultipartForm key value pairs
	MultipartForm MultipartForm

	// 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

	// 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) PrintCURL added in v1.0.11

func (c *Client) PrintCURL()

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{}

type MultipartForm added in v1.0.10

type MultipartForm struct {
	Fields map[string]string
	Files  []string
}

MultipartForm Fields is key value pairs, Files is a list of local files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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