curl

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 10 Imported by: 0

README

go-curl

MIT licensed

介绍

基于 Golang 内置 net/http 包中 http.Client 结构方法,实现 HTTP 客户端请求方式,可以直接使用 Get\Post\Put\Patch\Delete 方式发起 HTTP 请求,实现远程数据请求,使得开发过程调用更简单、便捷。

安装
// github
go get github.com/lihao1988/go-curl

// gitee
go get gitee.com/lihao1988/go-curl

版本要求

Go 1.15 or above.

HTTP 函数

1. Get Request
// example - 1
client := NewClient("http://www.example.com")
dataBytes, err := client.Get("/api?param=1", nil) // param url
fmt.Println("Get: ", string(dataBytes), err)

// example - 2
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"} 
dataBytes, err := client.Get("/api", data) // param rawQuery
fmt.Println("Get: ", string(dataBytes), err)

// example - 3
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.Curl("/api", Get, data, JsonType)
fmt.Println("Get: ", string(dataBytes), err)
2. Post Request
// example - 1
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.Post("/api", data) // json
fmt.Println("Post: ", string(dataBytes), err)

// example - 2
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.PostByForm("/api", data) // form
fmt.Println("Post: ", string(dataBytes), err)

// example - 3
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.Curl("/api", Post, data, FormType)
fmt.Println("Post: ", string(dataBytes), err)
3. Put Request
// example - or PutByForm\Curl
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.Put("/api", data) // json
fmt.Println("Put: ", string(dataBytes), err)
4. Patch Request
// example - or PatchByForm\Curl
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.Patch("/api", data) // json
fmt.Println("Patch: ", string(dataBytes), err)
5. Delete Request
// example - or Curl
client := NewClient("http://www.example.com")
data := map[string]string{"param":"1"}
dataBytes, err := client.Delete("/api", data)
fmt.Println("Delete: ", string(dataBytes), err)

Documentation

Index

Constants

View Source
const (
	// method
	Get    MethodType = "GET"
	Post   MethodType = "POST"
	Put    MethodType = "PUT"
	Patch  MethodType = "PATCH"
	Delete MethodType = "DELETE"

	// content_type
	JsonType  ContentType = "application/json"
	FormType  ContentType = "application/x-www-form-urlencoded"
	OtherType ContentType = "null" // need set header and body

)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client struct

func NewClient

func NewClient(host string) *Client

NewClient new client instance

func (*Client) Curl

func (c *Client) Curl(path string, method MethodType, data interface{}, cType ContentType) ([]byte, error)

Curl url request

func (*Client) Delete

func (c *Client) Delete(path string, data interface{}) ([]byte, error)

Delete request delete, content_type for "application/json"

func (*Client) Get

func (c *Client) Get(path string, data interface{}) ([]byte, error)

Get request get, content_type for "application/json"

func (*Client) Patch

func (c *Client) Patch(path string, data interface{}) ([]byte, error)

Patch request patch, content_type for "application/json"

func (*Client) PatchByForm

func (c *Client) PatchByForm(path string, data interface{}) ([]byte, error)

PatchByForm request patch, content_type for "application/x-www-form-urlencoded"

func (*Client) Post

func (c *Client) Post(path string, data interface{}) ([]byte, error)

Post request post, content_type for "application/json"

func (*Client) PostByForm

func (c *Client) PostByForm(path string, data interface{}) ([]byte, error)

PostByForm request post, content_type for "application/x-www-form-urlencoded"

func (*Client) Put

func (c *Client) Put(path string, data interface{}) ([]byte, error)

Put request put, content_type for "application/json"

func (*Client) PutByForm

func (c *Client) PutByForm(path string, data interface{}) ([]byte, error)

PutByForm request put, content_type for "application/x-www-form-urlencoded"

func (*Client) SetBody

func (c *Client) SetBody(body string) *Client

SetBody set request body

func (*Client) SetCookies

func (c *Client) SetCookies(cookies []*http.Cookie) *Client

SetCookies set request cookies

func (*Client) SetHeaders

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

SetHeaders set request headers

func (*Client) SetTimeout

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

SetTimeout set request timeout

type ContentType

type ContentType string

type MethodType

type MethodType string

request method

Jump to

Keyboard shortcuts

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