gohttp

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 9 Imported by: 0

README

gohttp

The http request client, which only supports returning json format.


Example of use

Standard CURD

Get, Delete request example.

	req := gohttp.Request{}
	req.SetURL("http://localhost:8080/user")
	req.SetHeaders(map[string]string{
		"Authorization": "Bearer token",
	})
	req.SetParams(gohttp.KV{
		"id": 123,
	})

	resp, err := req.GET()
	// resp, err := req.Delete()

	result := &gohttp.StdResult{} // other structures can be defined to receive data
	err = resp.BindJSON(result)

Post, Put, Patch request example.

	req := gohttp.Request{}
	req.SetURL("http://localhost:8080/user")
	req.SetHeaders(map[string]string{
		"Authorization": "Bearer token",
	})

	// body is a structure
    type User struct{
        Name string
        Email string
    }
    body := &User{"foo", "foo@bar.com"}
    req.SetJSONBody(body)
    // or body as json
    // req.SetBody(`{"name":"foo", "email":"foo@bar.com"}`)

	resp, err := req.Post()
	// resp, err := req.Put()
	// resp, err := req.Patch()

	result := &gohttp.StdResult{} // other structures can be defined to receive data
	err = resp.BindJSON(result)

simplified version of CRUD

No support for setting header, timeout, etc.

    url := "http://localhost:8080/user"
    params := gohttp.KV{"id":123}
    result := &gohttp.StdResult{} // other structures can be defined to receive data

    // Get
    err := gohttp.Get(result, url)
    err := gohttp.Get(result, url, params)

    // Delete
    err := gohttp.Delete(result, url)
    err := gohttp.Delete(result, url, params)

    type User struct{
        Name string
        Email string
    }
    body := &User{"foo", "foo@bar.com"}

    // Post
    err := gohttp.Post(result, url, body)
    // Put
    err := gohttp.Put(result, url, body)
    // Patch
    err := gohttp.Patch(result, url, body)

Documentation

Overview

Package gohttp is http request client, which only supports returning json format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(result interface{}, urlStr string, params ...KV) error

Delete request, return custom json format

func Get

func Get(result interface{}, urlStr string, params ...KV) error

Get request, return custom json format

func Patch

func Patch(result interface{}, urlStr string, body interface{}) error

Patch request, return custom json format

func Post

func Post(result interface{}, urlStr string, body interface{}) error

Post request, return custom json format

func Put

func Put(result interface{}, urlStr string, body interface{}) error

Put request, return custom json format

Types

type KV

type KV map[string]interface{}

KV string:interface{}

type Request

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

Request HTTP request

func (*Request) CustomRequest

func (req *Request) CustomRequest(f func(req *http.Request, data *bytes.Buffer)) *Request

CustomRequest customize request, e.g. add sign, set header, etc.

func (*Request) DELETE

func (req *Request) DELETE() (*Response, error)

DELETE send a DELETE request

func (*Request) Do

func (req *Request) Do(method string, data interface{}) (*Response, error)

Do a request

func (*Request) GET

func (req *Request) GET() (*Response, error)

GET send a GET request

func (*Request) PATCH

func (req *Request) PATCH() (*Response, error)

PATCH send PATCH requests

func (*Request) POST

func (req *Request) POST() (*Response, error)

POST send a POST request

func (*Request) PUT

func (req *Request) PUT() (*Response, error)

PUT send a PUT request

func (*Request) Reset

func (req *Request) Reset()

Reset set all fields to default value, use at pool

func (*Request) Response

func (req *Request) Response() (*Response, error)

Response return response

func (*Request) SetBody

func (req *Request) SetBody(body string) *Request

SetBody set body data

func (*Request) SetContentType

func (req *Request) SetContentType(a string) *Request

SetContentType set ContentType

func (*Request) SetHeader

func (req *Request) SetHeader(k, v string) *Request

SetHeader set the value of the request header

func (*Request) SetHeaders

func (req *Request) SetHeaders(headers map[string]string) *Request

SetHeaders set the value of Request Headers

func (*Request) SetJSONBody

func (req *Request) SetJSONBody(body interface{}) *Request

SetJSONBody set Body data, JSON format

func (*Request) SetParam

func (req *Request) SetParam(k string, v interface{}) *Request

SetParam parameters after setting the URL

func (*Request) SetParams

func (req *Request) SetParams(params map[string]interface{}) *Request

SetParams parameters after setting the URL

func (*Request) SetTimeout

func (req *Request) SetTimeout(t time.Duration) *Request

SetTimeout set timeout

func (*Request) SetURL

func (req *Request) SetURL(path string) *Request

SetURL set URL

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response HTTP response

func (*Response) BindJSON

func (resp *Response) BindJSON(v interface{}) error

BindJSON parses the response's body as JSON

func (*Response) BodyString

func (resp *Response) BodyString() (string, error)

BodyString returns the body data of the HttpResponse

func (*Response) Error

func (resp *Response) Error() error

Error return err

func (*Response) ReadBody

func (resp *Response) ReadBody() ([]byte, error)

ReadBody returns the body data of the HttpResponse

type StdResult

type StdResult struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

StdResult standard return data

Jump to

Keyboard shortcuts

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