curl

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

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

Go to latest
Published: Apr 20, 2021 License: GPL-3.0 Imports: 11 Imported by: 0

README

curl

基于http.Client实现的curl客户端,提供更为快捷的http接口调用方式。

安装

go get github.com/ebar-go/curl

使用说明

GET请求

package main
import (
	"fmt"
	"github.com/ebar-go/curl"
)
func main() {
    var address = "http://localhost:8080/api"

    response, err := curl.Get(address)
    if err != nil {
        panic(err)
    }
    fmt.Println(response.String()) // 可以通过response获取string类型的响应
    fmt.Println(response.Byte()) // 也可以是byte
    var obj struct{}
    err = response.BindJson(&obj) // 也可以解析到一个结构体
}

POST请求

params := make(url.Values)
params.Set("id", "1")
response, err := curl.Post("localhost:8080/test", strings.NewReader(params.Encode()))

POST JSON请求

body := strings.NewReader(`{"username":"curl"}`)
response, err := curl.PostJson("localhost:8080/test", body)

PUT请求

response, err := curl.Put(address, nil)

PUT请求

response, err := curl.Put(address, nil)

Patch请求

response, err := curl.Patch(address, nil)

Delete请求

response, err := curl.Delete(address, nil)

自定义请求

request, _ := http.NewRequest(http.MethodPost, url, body)
response, err := curl.Send(request)
if err != nil {
    return err
}
fmt.Println(response.String())

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Curl

type Curl interface {
	// send get request
	Get(url string) (Response, error)
	// send post request
	Post(url string, body io.Reader) (Response, error)
	// send put request
	Put(url string, body io.Reader) (Response, error)
	// send patch request
	Patch(url string, body io.Reader) (Response, error)
	// send delete request
	Delete(url string) (Response, error)
	// post file
	PostFile(url string, files map[string]string, params map[string]string) (Response, error)
	// send http request
	Send(request *http.Request) (Response, error)
	// send post json request
	PostJson(url string, body io.Reader) (Response, error)
}

Curl

func Default

func Default() Curl

Default return default instance of Curl

func New

func New(opts ...Option) Curl

New return curl instance

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithHttpTransport

func WithHttpTransport(transport *http.Transport) Option

WithHttpTransport set http transport option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout set http client timeout option

type Response

type Response interface {
	// get string
	String() string
	// get byte
	Byte() []byte
	// json marshal
	BindJson(object interface{}) error
	// get reader
	Reader() io.Reader
}

Response

func Delete

func Delete(url string) (Response, error)

Delete send delete request

func Get

func Get(url string) (Response, error)

Get send get request

func Patch

func Patch(url string, body io.Reader) (Response, error)

Patch send patch request

func Post

func Post(url string, body io.Reader) (Response, error)

Post send post request

func PostFile

func PostFile(url string, files map[string]string, params map[string]string) (Response, error)

PostFile upload file by post request

func PostJson

func PostJson(url string, body io.Reader) (Response, error)

PostJson send post request with content-type: application/json

func Put

func Put(url string, body io.Reader) (Response, error)

Put send put request

func Send

func Send(request *http.Request) (Response, error)

Send send custom http request

Jump to

Keyboard shortcuts

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