httpClient

package module
v0.0.0-...-bdda7b7 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2018 License: MIT Imports: 14 Imported by: 0

README

GOLANG LIBRARY TOOLS

  1. httpclient
    httpclient library very simple chaining method & support ssl
    example:

package main

import (
      "encoding/json"
      "time"
      
      "brainlabs/httpClient"
)

func main(){
      
    client := httpClient.NewClient()      
    rsp, err := client.
                        SetHeader("Content-Type", "application/json").
                        SetHeader("Channel", "tester").
                        SetTimeout(3 * time.Second).
                        //SetPemCertificate("cert/cert_rsa.pub"). // if use ssl
                        Get("https://httpstat.us/200?sleep=5000")
    
    if err != nil && rsp.IsTimeout() {
    		fmt.Println(err)
    		return
    }
    
    if err != nil {
    		fmt.Println(err)
    		return
    }
    
    // get raw response
    fmt.Println(rsp.GetRaw())
    
   
    var testData = struct {
    		ID   string `json:"id"`
    		Name string `json:"name"`
    }{}
    
    err := rsp.GetUnmarshalJSON(&testData)
    
    if err != nil {
    		fmt.Println(err)
    }
    
    x, _ := json.MarshalIndent(testData, "", " ")
    
    fmt.Println(string(x))

}

Author

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultMaxIdleConns        = 100
	DefaultMaxIdleConnsPerHost = 100
	DefaultRequestTimeout      = 10000 * time.Millisecond
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Transport *http.Transport

	Cookie http.CookieJar
	// contains filtered or unexported fields
}

Client http request very simple

func NewClient

func NewClient() *Client

NewClient http wrapper

func (*Client) Delete

func (c *Client) Delete(url string, data []byte) (*Response, error)

Delete method handle http client request

func (*Client) Get

func (c *Client) Get(url string) (*Response, error)

Get method handle http client request

func (*Client) Post

func (c *Client) Post(url string, data []byte) (*Response, error)

Post method handle http client request

func (*Client) Put

func (c *Client) Put(url string, data []byte) (*Response, error)

Put method handle http client request

func (*Client) Request

func (c *Client) Request(method, url string, payload []byte) (*Response, error)

Request handle http client request

func (*Client) SetHeader

func (c *Client) SetHeader(key, value string) *Client

SetHeader handle http client request header

func (*Client) SetHeaders

func (c *Client) SetHeaders(headers map[string]string) *Client

SetHeaders handle http client request headers

func (*Client) SetPemCertificate

func (c *Client) SetPemCertificate(pemFile string) *Client

SetPemCertificate http client request use ssl

func (*Client) SetQuery

func (c *Client) SetQuery(query map[string]string) *Client

SetQuery uri url string http client request

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration) *Client

SetTimeout handles http client request timeout

type Response

type Response struct {
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(rsp *http.Response) *Response

func (*Response) GetAsString

func (r *Response) GetAsString() (string, error)

GetAsString http response client

func (*Response) GetHeader

func (r *Response) GetHeader(key string) string

GetHeader http response client

func (*Response) GetRaw

func (r *Response) GetRaw() io.Reader

func (*Response) GetStatusCode

func (r *Response) GetStatusCode() int

GetStatusCode http client response

func (*Response) GetUnmarshalJSON

func (r *Response) GetUnmarshalJSON(v interface{}) error

GetFromJSON response http client

func (*Response) GetUnmarshalXML

func (r *Response) GetUnmarshalXML(v interface{}) error

GetFromXML response http client

func (*Response) IsTimeout

func (r *Response) IsTimeout() bool

IsTimeout request response

Jump to

Keyboard shortcuts

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