request

package module
v1.0.5-cookiejar Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 7 Imported by: 0

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
  • 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(resp.Code, string(resp.Data), 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(resp.Code, string(resp.Data), 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(resp.Code, string(resp.Data), 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(resp.Code, string(resp.Data), 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(resp.Code, string(resp.Data), err)
}
Cookies
package main

import (
    "log"

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

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

    resp, err := client.Do()

    log.Println(resp.Code, string(resp.Data), err)
}
TLS
package main

import (
    "log"
    "crypto/tls"

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

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

    resp, err := client.Do()

    log.Println(resp.Code, string(resp.Data), 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()

Cookies

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

resp, err := client.Do()

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

Variables

This section is empty.

Functions

This section is empty.

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
	ProxyURL    string
	ContentType ContentType
	Cookies     []*http.Cookie
	Jar         *cookiejar.Jar
	TLSConfig   *tls.Config
	Transport   *http.Transport
	// contains filtered or unexported fields
}

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() (resp SugaredResp, err error)

Do send http request

func (*Client) Resp

func (c *Client) Resp() (resp *http.Response, err error)

Resp do request and get original http response struct

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

type Method

type Method string

Method http method TODO:

type SugaredResp

type SugaredResp struct {
	Data []byte
	Code int
	// contains filtered or unexported fields
}

SugaredResp Sugared response with status code and body data

func (*SugaredResp) Close

func (s *SugaredResp) Close()

Close close response body

func (*SugaredResp) Status

func (s *SugaredResp) Status() (status string)

Status get response status code and text, like 200 ok

func (*SugaredResp) StatusCode

func (s *SugaredResp) StatusCode() (code int)

StatusCode get response status code

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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