gohttp

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 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.

    import "github.com/zhufuyi/sponge/pkg/gohttp"

    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.

    import "github.com/zhufuyi/sponge/pkg/gohttp"

    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.

    import "github.com/zhufuyi/sponge/pkg/gohttp"

    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. Deprecated: moved to pkg/httpcli, will remove in future version.

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 Deprecated: moved to pkg/httpcli Delete

func Get

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

Get request, return custom json format Deprecated: moved to pkg/httpcli Get

func Patch

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

Patch request, return custom json format Deprecated: moved to pkg/httpcli Patch

func Post

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

Post request, return custom json format Deprecated: moved to pkg/httpcli Post

func Put

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

Put request, return custom json format Deprecated: moved to pkg/httpcli Put

Types

type KV

type KV = map[string]interface{}

KV string:interface{} Deprecated: moved to pkg/httpcli KV

type Request

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

Request HTTP request Deprecated: moved to pkg/httpcli 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. Deprecated: moved to pkg/httpcli CustomRequest

func (*Request) DELETE

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

DELETE send a DELETE request Deprecated: moved to pkg/httpcli DELETE

func (*Request) Do

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

Do a request Deprecated: moved to pkg/httpcli Do

func (*Request) GET

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

GET send a GET request Deprecated: moved to pkg/httpcli GET

func (*Request) PATCH

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

PATCH send PATCH requests Deprecated: moved to pkg/httpcli PATCH

func (*Request) POST

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

POST send a POST request Deprecated: moved to pkg/httpcli POST

func (*Request) PUT

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

PUT send a PUT request Deprecated: moved to pkg/httpcli PUT

func (*Request) Reset

func (req *Request) Reset()

Reset set all fields to default value, use at pool Deprecated: moved to pkg/httpcli Reset

func (*Request) Response

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

Response return response Deprecated: moved to pkg/httpcli Response

func (*Request) SetBody

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

SetBody set body data Deprecated: moved to pkg/httpcli SetBody

func (*Request) SetContentType

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

SetContentType set ContentType Deprecated: moved to pkg/httpcli SetContentType

func (*Request) SetHeader

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

SetHeader set the value of the request header Deprecated: moved to pkg/httpcli SetHeader

func (*Request) SetHeaders

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

SetHeaders set the value of Request Headers Deprecated: moved to pkg/httpcli SetHeaders

func (*Request) SetJSONBody

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

SetJSONBody set Body data, JSON format Deprecated: moved to pkg/httpcli SetJSONBody

func (*Request) SetParam

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

SetParam parameters after setting the URL Deprecated: moved to pkg/httpcli SetParam

func (*Request) SetParams

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

SetParams parameters after setting the URL Deprecated: moved to pkg/httpcli SetParams

func (*Request) SetTimeout

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

SetTimeout set timeout Deprecated: moved to pkg/httpcli SetTimeout

func (*Request) SetURL

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

SetURL set URL Deprecated: moved to pkg/httpcli SetURL

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 Deprecated: moved to pkg/httpcli BindJSON

func (*Response) BodyString

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

BodyString returns the body data of the HttpResponse Deprecated: moved to pkg/httpcli BodyString

func (*Response) Error

func (resp *Response) Error() error

Error return err Deprecated: moved to pkg/httpcli Error

func (*Response) ReadBody

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

ReadBody returns the body data of the HttpResponse Deprecated: moved to pkg/httpcli ReadBody

type StdResult

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

StdResult standard return data Deprecated: moved to pkg/httpcli StdResult

Jump to

Keyboard shortcuts

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