request

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 5 Imported by: 35

README

Request

Build Status GoDoc Go Report Card codecov Release TODOs Sourcegraph Open Source Helpers

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
  • Intercept request and response
  • 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

GET
package main

import (
    "log"

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

func main() {
    client := request.Client{
        URL:    "https://google.com",
        Method: "GET",
        Params: map[string]string{"hello": "world"},
    }
    resp, err := client.Do()

    log.Println(string(resp), err)
}
POST
package main

import (
    "log"

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

func main() {
    client := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Params: map[string]string{"hello": "world"},
        Body:   []byte(`{"hello": "world"}`),
    }
    resp, err := client.Do()

    log.Println(string(resp), err)
}
Content-Type
package main

import (
    "log"

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

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

    log.Println(string(resp), err)
}
Authorization
package main

import (
    "log"

    "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",
        }, // xxx:xxx
    }

    resp, err := client.Do()

    log.Println(string(resp), err)
}
Timeout
package main

import (
    "log"

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

func main() {
    client := request.Client{
        URL:       "https://google.com",
        Method:    "POST",
        Timeout:   10, // seconds
    }

    resp, err := client.Do()

    log.Println(string(resp), err)
}

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",
    Params: map[string]string{"hello": "world"},
}
resp, err := client.Do()

POST

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

Content-Type

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

Authorization

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

resp, err := client.Do()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeURL

func EncodeURL(baseURL string, p map[string]string) (string, error)

EncodeURL add and encoded parameters.

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

BasicAuth Add Username:Password as Basic Auth

type Client

type Client struct {
	URL         string
	Method      string
	Header      map[string]string
	Params      map[string]string
	Body        []byte
	BasicAuth   BasicAuth
	Timeout     time.Duration // second
	ContentType ContentType
}

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) Do

func (c *Client) Do() ([]byte, error)

Do send http request

type ContentType

type ContentType string

ContentType Content-Type

const (
	// ApplicationJSON application/json
	ApplicationJSON ContentType = "application/json"

	// ApplicationXWwwFormURLEncoded application/x-www-form-urlencoded
	ApplicationXWwwFormURLEncoded ContentType = "application/x-www-form-urlencoded"

	// MultipartFormData multipart/form-data
	MultipartFormData ContentType = "multipart/form-data"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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