Documentation ¶
Index ¶
- type ContentType
- type HttpClient
- func (h *HttpClient) AddCookie(cookie *http.Cookie) *HttpClient
- func (h *HttpClient) AddHeader(name, value string) *HttpClient
- func (h *HttpClient) AddHeaders(headers map[string]string) *HttpClient
- func (h *HttpClient) FastGet(url string) (body []byte, err error)
- func (h *HttpClient) FastPost(url string, data map[string][]string) (body []byte, err error)
- func (h *HttpClient) MultipartForm(params map[string][]string, files map[string]string) *HttpClient
- func (h *HttpClient) Request(url string) (body []byte, err error)
- func (h *HttpClient) RequestStream(url string) (body io.Writer, err error)
- func (h *HttpClient) ResponseHeaders() map[string][]string
- func (h *HttpClient) SetBody(body []byte) *HttpClient
- func (h *HttpClient) SetBodyMap(body map[string][]string) *HttpClient
- func (h *HttpClient) SetBodyStream(body io.Reader) *HttpClient
- func (h *HttpClient) SetContentType(contentType ContentType) *HttpClient
- func (h *HttpClient) SetMethod(method HttpMethod) *HttpClient
- func (h *HttpClient) SetUserAgent(userAgent string) *HttpClient
- type HttpMethod
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContentType ¶
type ContentType string
ContentType 请求头Content-Type的值
const ( FormUrlencoded ContentType = "application/x-www-form-urlencoded" FormData ContentType = "multipart/form-data" Json ContentType = "application/json" )
func (ContentType) ToString ¶
func (c ContentType) ToString() string
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
HttpClient http客户端,不要手动构建该实例,应该调用NewHttpClient函数创建实例
func NewHttpClient ¶
func NewHttpClient() *HttpClient
NewHttpClient 创建HttpClient实例 HttpClient中的Set、Add方法支持链式调用方式,最后通过Reqeust方法发送请求 默认情况下,会将Content-Type设置为application/x-www-form-urlencoded
func (*HttpClient) AddCookie ¶
func (h *HttpClient) AddCookie(cookie *http.Cookie) *HttpClient
func (*HttpClient) AddHeader ¶
func (h *HttpClient) AddHeader(name, value string) *HttpClient
AddHeader 添加请求头
func (*HttpClient) AddHeaders ¶
func (h *HttpClient) AddHeaders(headers map[string]string) *HttpClient
AddHeaders 批量添加请求头
func (*HttpClient) FastGet ¶
func (h *HttpClient) FastGet(url string) (body []byte, err error)
FastGet 发送简单的GET请求,注意,调用该方法发送请求后 ResponseHeaders方法不会获取响应头
url: 请求地址
return
body: 响应内容 err: 请求过程中出现的错误
func (*HttpClient) FastPost ¶
FastPost 发送简单的POST请求,注意,调用该方法发送请求后 ResponseHeaders方法不会获取响应头
url: 请求地址 data: post请求参数
return
body: 响应内容 err: 请求过程中出现的错误
func (*HttpClient) MultipartForm ¶
func (h *HttpClient) MultipartForm(params map[string][]string, files map[string]string) *HttpClient
MultipartForm 用于发送multipart/form-data类型的数据,会将Content-Type设置为multipart/form-data
params: 请求参数 files: 上传文件
func (*HttpClient) Request ¶
func (h *HttpClient) Request(url string) (body []byte, err error)
Request 发送请求,调用成功后可使用 ResponseHeaders方法获取响应头
url: 请求地址
return
body: 响应内容 err: 请求过程中出现的错误
func (*HttpClient) RequestStream ¶
func (h *HttpClient) RequestStream(url string) (body io.Writer, err error)
RequestStream 发送请求,调用成功后可使用 ResponseHeaders方法获取响应头
url: 请求地址
return
body: 响应内容 err: 请求过程中出现的错误
func (*HttpClient) ResponseHeaders ¶
func (h *HttpClient) ResponseHeaders() map[string][]string
ResponseHeaders 请求完成后,使用此方法获取响应头
func (*HttpClient) SetBodyMap ¶
func (h *HttpClient) SetBodyMap(body map[string][]string) *HttpClient
SetBodyMap 设置请求体内容,通常post请求参数可以放置在此处
func (*HttpClient) SetBodyStream ¶
func (h *HttpClient) SetBodyStream(body io.Reader) *HttpClient
SetBodyStream 设置请求体内容
func (*HttpClient) SetContentType ¶
func (h *HttpClient) SetContentType(contentType ContentType) *HttpClient
SetContentType 设置Content-Type
func (*HttpClient) SetMethod ¶
func (h *HttpClient) SetMethod(method HttpMethod) *HttpClient
SetMethod 设置请求方法,GET、POST、DELETE等
func (*HttpClient) SetUserAgent ¶
func (h *HttpClient) SetUserAgent(userAgent string) *HttpClient
SetUserAgent 设置User-Agent
type HttpMethod ¶
type HttpMethod string
HttpMethod http请求类型,post、get等等
const ( HttpGet HttpMethod = "GET" HttpHead HttpMethod = "HEAD" HttpPost HttpMethod = "POST" HttpPut HttpMethod = "PUT" HttpPatch HttpMethod = "PATCH" HttpDelete HttpMethod = "DELETE" HttpConnect HttpMethod = "CONNECT" HttpOptions HttpMethod = "OPTIONS" HttpTrace HttpMethod = "TRACE" )
func (HttpMethod) ToString ¶
func (h HttpMethod) ToString() string