nativehttpclient

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2020 License: MIT Imports: 13 Imported by: 0

README

NATIVE-HTTP-CLIENT

通过 tcp 和 unix sock 文件发送原生HTTP请求的HTTP客户端

开发计划
  • HTTP
  • 302
  • 公开 test api
  • HTTPS
  • TLS
  • HTTP2
获取实例

连接到 TCP

client := NewTcp("domain")

连接到 Socket 文件

client := NewUnixSock("/var/run/docker.sock")
请求

GET、DELETE、HEAD、OPTIONS

第二个参数将会被处理成 QueryString,追加到url后面。例如:/containers/json?sort=id

response, err := client.Get("/containers/json", map[string]string{
     "sort": "id",
 })
response, err := client.Delete("/containers/json", nil)
response, err := client.Head("/containers/json", nil)
response, err := client.Options("/containers/json", nil)

POST、PUT、PATCH

同时接受字符串和其它参数类型(将会被格式化为json)

response, err := client.Post("/containers/create", map[string]string{
    "Image": "nginx",
})
response, err := client.Put("/containers/create", `{"Image":"nginx"}`)
response, err :=  client.Patch("/containers/create", `{"Image":"nginx"}`)
原始 HTTP 协议

调用请求API,将会生成类似如下两种的原始 HTTP 协议进行发送,其中的GET和POST将会被替换成实际协议。

GET、DELETE、HEAD、OPTIONS

GET /containers/json HTTP/1.1
Host: domain:80
Connection: close

POST、PUT、PATCH

POST /containers/create?name=nginx HTTP/1.1
Host: localhost
Connection: close
Content-Length: 17
Accept: */*
Content-Type: application/json;charset=UTF-8
User-Agent: native-http-client
Cookie: User-Agent=native-http-client; 

{"Image":"nginx"}
响应

response 为字符串格式的原始响应值,例如 Json xml 等,可直接使用

response, err := client.Get("/containers/json", nil)
response, err := client.Put("/containers/create", `{"Image":"nginx"}`)
API

携带 Header

client.WithHeader("User-Agent", "native-http-client")
      .WithHeader("Token", "***")
      .WithHeader("...", "***")

携带 Cookie
仅当 Header 中未设置 Cookie 字段时生效

client.WithCookie("UserName", "alex")
      .WithCookie("UID", "***")
      .WithCookie("...", "***")

更多

响应:client.Response

{
    "ContentType": "application/json",
    "ContentLength": 1054,
    "StatusCode": 200,
    "Headers": {
        "Connection": "close",
        "Content-Length": "1054",
        "Content-Type": "application/json",
        "Date": "Wed, 14 Oct 2020 08:37:20 GMT",
        "Server": "nginx/1.18.0"
    },
    "Cookies": {},
    "Body": "{\"Status\":\"OK\",\"...\":\"...\"}"
}
核心原理

对网络发送原始HTTP协议,以下为 POST API 对应的命令示例

unix sock

curl -s --unix-socket /var/run/docker.sock \
    -X POST http://localhost/containers/create?name=nginx \
    --data '{"Image":"nginx"}' 

tcp

telnet domain 80
Trying xxxxxx...
Connected to domain.
Escape character is '^]'.
POST /containers/create?name=nginx HTTP/1.1
Host: localhost
Connection: close
Content-Length: 17
Accept: */*
Content-Type: application/json;charset=UTF-8
User-Agent: native-http-client
Cookie: User-Agent=native-http-client; 

{"Image":"nginx"}
鸣谢

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UserAgent = "Native_Http_Client/0.5.1"

Functions

func InitLog added in v0.5.0

func InitLog()

func StringTemplate

func StringTemplate(body string, data interface{}) string

Types

type Configs added in v0.3.0

type Configs struct {
	MaxRedirects uint
}

type Data

type Data struct {
	Method  string
	Route   string
	Host    string
	Length  int
	Headers string
	Body    string
}

type HttpClient

type HttpClient struct {
	Network  string
	Address  string
	Headers  map[string]string
	Cookies  map[string]string
	Request  Request
	Response Response
	Configs  Configs
}

func Defaults added in v0.2.0

func Defaults() *HttpClient

func NewTcp

func NewTcp(link string) *HttpClient

func NewUnixSock

func NewUnixSock(file string) *HttpClient

func (*HttpClient) Delete

func (h *HttpClient) Delete(url string, body interface{}) (*Response, error)

func (*HttpClient) Do

func (h *HttpClient) Do(method string, route string, body interface{}) (*Response, error)

func (*HttpClient) Get

func (h *HttpClient) Get(route string, params map[string]string) (*Response, error)

func (*HttpClient) Head

func (h *HttpClient) Head(url string, body interface{}) (*Response, error)

func (*HttpClient) Options

func (h *HttpClient) Options(url string, body interface{}) (*Response, error)

func (*HttpClient) Patch

func (h *HttpClient) Patch(url string, body interface{}) (*Response, error)

func (*HttpClient) Post

func (h *HttpClient) Post(url string, body interface{}) (*Response, error)

func (*HttpClient) Put

func (h *HttpClient) Put(url string, body interface{}) (*Response, error)

func (*HttpClient) WithCookie

func (h *HttpClient) WithCookie(key string, value string) *HttpClient

func (*HttpClient) WithHeader

func (h *HttpClient) WithHeader(key string, value string) *HttpClient

type Request added in v0.2.0

type Request struct {
	Origin  string
	Payload string
}

type Response added in v0.2.0

type Response struct {
	ContentType   string
	ContentLength uint
	StatusCode    uint
	Headers       map[string]string
	Cookies       map[string]string
	Body          string
	Origin        string
}

func (*Response) BodyFormat added in v0.3.0

func (r *Response) BodyFormat(declare interface{}) error

Jump to

Keyboard shortcuts

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