goz

package module
Version: v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 22 Imported by: 16

README

goz

A fantastic HTTP request library used in golang. Inspired by guzzle

Installation

go get -u github.com/idoubi/goz

Documentation

API documentation can be found here: https://godoc.org/github.com/idoubi/goz

Basic Usage

package main

import (
    "github.com/idoubi/goz"
)

func main() {
    cli := goz.NewClient()

	resp, err := cli.Get("http://127.0.0.1:8091/get")
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("%T", resp)
	// Output: *goz.Response
}

Query Params

  • query map
cli := goz.NewClient()

resp, err := cli.Get("http://127.0.0.1:8091/get-with-query", goz.Options{
    Query: map[string]interface{}{
        "key1": "value1",
        "key2": []string{"value21", "value22"},
        "key3": "333",
    },
})
if err != nil {
    log.Fatalln(err)
}

fmt.Printf("%s", resp.GetRequest().URL.RawQuery)
// Output: key1=value1&key2=value21&key2=value22&key3=333
  • query string
cli := goz.NewClient()

resp, err := cli.Get("http://127.0.0.1:8091/get-with-query?key0=value0", goz.Options{
    Query: "key1=value1&key2=value21&key2=value22&key3=333",
})
if err != nil {
    log.Fatalln(err)
}

fmt.Printf("%s", resp.GetRequest().URL.RawQuery)
// Output: key1=value1&key2=value21&key2=value22&key3=333

Post Data

  • post form
cli := goz.NewClient()

resp, err := cli.Post("http://127.0.0.1:8091/post-with-form-params", goz.Options{
    Headers: map[string]interface{}{
        "Content-Type": "application/x-www-form-urlencoded",
    },
    FormParams: map[string]interface{}{
        "key1": "value1",
        "key2": []string{"value21", "value22"},
        "key3": "333",
    },
})
if err != nil {
    log.Fatalln(err)
}

body, _ := resp.GetBody()
fmt.Println(body)
// Output: form params:{"key1":["value1"],"key2":["value21","value22"],"key3":["333"]}
  • post json
cli := goz.NewClient()

resp, err := cli.Post("http://127.0.0.1:8091/post-with-json", goz.Options{
    Headers: map[string]interface{}{
        "Content-Type": "application/json",
    },
    JSON: struct {
        Key1 string   `json:"key1"`
        Key2 []string `json:"key2"`
        Key3 int      `json:"key3"`
    }{"value1", []string{"value21", "value22"}, 333},
})
if err != nil {
    log.Fatalln(err)
}

body, _ := resp.GetBody()
fmt.Println(body)
// Output: json:{"key1":"value1","key2":["value21","value22"],"key3":333}

Request Headers

cli := goz.NewClient()

resp, err := cli.Post("http://127.0.0.1:8091/post-with-headers", goz.Options{
    Headers: map[string]interface{}{
        "User-Agent": "testing/1.0",
        "Accept":     "application/json",
        "X-Foo":      []string{"Bar", "Baz"},
    },
})
if err != nil {
    log.Fatalln(err)
}

headers := resp.GetRequest().Header["X-Foo"]
fmt.Println(headers)
// Output: [Bar Baz]

Response

cli := goz.NewClient()
resp, err := cli.Get("http://127.0.0.1:8091/get")
if err != nil {
    log.Fatalln(err)
}

body, err := resp.GetBody()
if err != nil {
    log.Fatalln(err)
}
fmt.Printf("%T", body)
// Output: goz.ResponseBody

part := body.Read(30)
fmt.Printf("%T", part)
// Output: []uint8

contents := body.GetContents()
fmt.Printf("%T", contents)
// Output: string

fmt.Println(resp.GetStatusCode())
// Output: 200

fmt.Println(resp.GetReasonPhrase())
// Output: OK

headers := resp.GetHeaders()
fmt.Printf("%T", headers)
// Output: map[string][]string

flag := resp.HasHeader("Content-Type")
fmt.Printf("%T", flag)
// Output: bool

header := resp.GetHeader("content-type")
fmt.Printf("%T", header)
// Output: []string
    
headerLine := resp.GetHeaderLine("content-type")
fmt.Printf("%T", headerLine)
// Output: string

Proxy

cli := goz.NewClient()

resp, err := cli.Get("https://www.fbisb.com/ip.php", goz.Options{
    Timeout: 5.0,
    Proxy:   "http://127.0.0.1:1087",
})
if err != nil {
    log.Fatalln(err)
}

fmt.Println(resp.GetStatusCode())
// Output: 200

Timeout

cli := goz.NewClient(goz.Options{
    Timeout: 0.9,
})
resp, err := cli.Get("http://127.0.0.1:8091/get-timeout")
if err != nil {
    if resp.IsTimeout() {
        fmt.Println("timeout")
        // Output: timeout
        return
    }
}

fmt.Println("not timeout")

License

MIT

Copyright (c) 2017-present, idoubi

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FormData added in v1.3.3

type FormData struct {
	Name     string
	Contents []byte
	Filename string
	Filepath string
	Headers  map[string]interface{}
}

FormData: multipart form-data

type Options added in v1.0.0

type Options struct {
	Debug   bool
	BaseURI string
	Timeout float32

	Query        interface{}
	Headers      map[string]interface{}
	Cookies      interface{}
	FormParams   map[string]interface{}
	JSON         interface{}
	XML          interface{}
	Multipart    []FormData
	Proxy        string
	Certificates []tls.Certificate
	// contains filtered or unexported fields
}

Options object

type Request

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

Request object

func NewClient added in v1.0.0

func NewClient(opts ...Options) *Request

NewClient new request object

func (*Request) Delete added in v1.1.0

func (r *Request) Delete(uri string, opts ...Options) (*Response, error)

Delete send delete request

func (*Request) Get

func (r *Request) Get(uri string, opts ...Options) (*Response, error)

Get send get request

func (*Request) Options added in v1.1.0

func (r *Request) Options(uri string, opts ...Options) (*Response, error)

Options send options request

func (*Request) Patch added in v1.1.0

func (r *Request) Patch(uri string, opts ...Options) (*Response, error)

Patch send patch request

func (*Request) Post

func (r *Request) Post(uri string, opts ...Options) (*Response, error)

Post send post request

func (*Request) Put added in v1.1.0

func (r *Request) Put(uri string, opts ...Options) (*Response, error)

Put send put request

func (*Request) Request added in v1.0.0

func (r *Request) Request(method, uri string, opts ...Options) (*Response, error)

Request send request

func (*Request) SetOptions added in v1.3.0

func (r *Request) SetOptions(opts Options)

SetOptions: set request options

type Response

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

Response response object

func Delete added in v1.1.0

func Delete(uri string, opts ...Options) (*Response, error)

Delete send delete request

func Get added in v1.1.0

func Get(uri string, opts ...Options) (*Response, error)

Get send get request

func Patch added in v1.1.0

func Patch(uri string, opts ...Options) (*Response, error)

Patch send patch request

func Post added in v1.1.0

func Post(uri string, opts ...Options) (*Response, error)

Post send post request

func Put added in v1.1.0

func Put(uri string, opts ...Options) (*Response, error)

Put send put request

func (*Response) GetBody added in v1.0.0

func (r *Response) GetBody() (ResponseBody, error)

GetBody parse response body

func (*Response) GetHeader added in v1.0.0

func (r *Response) GetHeader(name string) []string

GetHeader get response header

func (*Response) GetHeaderLine added in v1.0.0

func (r *Response) GetHeaderLine(name string) string

GetHeaderLine get a single response header

func (*Response) GetHeaders added in v1.0.0

func (r *Response) GetHeaders() map[string][]string

GetHeaders get response headers

func (*Response) GetParsedBody added in v1.1.0

func (r *Response) GetParsedBody() (*gjson.Result, error)

GetParsedBody parse response body with gjson

func (*Response) GetReasonPhrase added in v1.0.0

func (r *Response) GetReasonPhrase() string

GetReasonPhrase get response reason phrase

func (*Response) GetRequest added in v1.0.0

func (r *Response) GetRequest() *http.Request

GetRequest get request object

func (*Response) GetStatusCode added in v1.0.0

func (r *Response) GetStatusCode() int

GetStatusCode get response status code

func (*Response) HasHeader added in v1.0.0

func (r *Response) HasHeader(name string) bool

HasHeader get if header exsits in response headers

func (*Response) IsTimeout added in v1.0.0

func (r *Response) IsTimeout() bool

IsTimeout get if request is timeout

type ResponseBody added in v1.0.0

type ResponseBody []byte

ResponseBody response body

func (ResponseBody) GetContents added in v1.0.0

func (r ResponseBody) GetContents() string

GetContents format response body as string

func (ResponseBody) Read added in v1.0.0

func (r ResponseBody) Read(length int) []byte

Read get slice of response body

func (ResponseBody) String added in v1.0.0

func (r ResponseBody) String() string

String fmt outout

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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