181 lines
3.4 KiB
Go
181 lines
3.4 KiB
Go
|
|
package starnet
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"net/http"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// SetHeader 设置 Header(覆盖)
|
|||
|
|
func (r *Request) SetHeader(key, value string) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Headers.Set(key, value)
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AddHeader 添加 Header
|
|||
|
|
func (r *Request) AddHeader(key, value string) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Headers.Add(key, value)
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetHeaders 设置所有 Headers(覆盖)
|
|||
|
|
func (r *Request) SetHeaders(headers http.Header) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Headers = headers
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AddHeaders 批量添加 Headers
|
|||
|
|
func (r *Request) AddHeaders(headers map[string]string) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
for k, v := range headers {
|
|||
|
|
r.config.Headers.Add(k, v)
|
|||
|
|
}
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// DeleteHeader 删除 Header
|
|||
|
|
func (r *Request) DeleteHeader(key string) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Headers.Del(key)
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetHeader 获取 Header
|
|||
|
|
func (r *Request) GetHeader(key string) string {
|
|||
|
|
return r.config.Headers.Get(key)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Headers 获取所有 Headers
|
|||
|
|
func (r *Request) Headers() http.Header {
|
|||
|
|
return r.config.Headers
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetContentType 设置 Content-Type
|
|||
|
|
func (r *Request) SetContentType(contentType string) *Request {
|
|||
|
|
return r.SetHeader("Content-Type", contentType)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetUserAgent 设置 User-Agent
|
|||
|
|
func (r *Request) SetUserAgent(userAgent string) *Request {
|
|||
|
|
return r.SetHeader("User-Agent", userAgent)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetReferer 设置 Referer
|
|||
|
|
func (r *Request) SetReferer(referer string) *Request {
|
|||
|
|
return r.SetHeader("Referer", referer)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetBearerToken 设置 Bearer Token
|
|||
|
|
func (r *Request) SetBearerToken(token string) *Request {
|
|||
|
|
return r.SetHeader("Authorization", "Bearer "+token)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AddCookie 添加 Cookie
|
|||
|
|
func (r *Request) AddCookie(cookie *http.Cookie) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Cookies = append(r.config.Cookies, cookie)
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AddSimpleCookie 添加简单 Cookie(path 为 /)
|
|||
|
|
func (r *Request) AddSimpleCookie(name, value string) *Request {
|
|||
|
|
return r.AddCookie(&http.Cookie{
|
|||
|
|
Name: name,
|
|||
|
|
Value: value,
|
|||
|
|
Path: "/",
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AddCookieKV 添加 Cookie(指定 path)
|
|||
|
|
func (r *Request) AddCookieKV(name, value, path string) *Request {
|
|||
|
|
return r.AddCookie(&http.Cookie{
|
|||
|
|
Name: name,
|
|||
|
|
Value: value,
|
|||
|
|
Path: path,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetCookies 设置所有 Cookies(覆盖)
|
|||
|
|
func (r *Request) SetCookies(cookies []*http.Cookie) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Cookies = cookies
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// AddCookies 批量添加 Cookies
|
|||
|
|
func (r *Request) AddCookies(cookies map[string]string) *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
if r.doRaw {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
for name, value := range cookies {
|
|||
|
|
r.config.Cookies = append(r.config.Cookies, &http.Cookie{
|
|||
|
|
Name: name,
|
|||
|
|
Value: value,
|
|||
|
|
Path: "/",
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Cookies 获取所有 Cookies
|
|||
|
|
func (r *Request) Cookies() []*http.Cookie {
|
|||
|
|
return r.config.Cookies
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ResetHeaders 重置所有 Headers
|
|||
|
|
func (r *Request) ResetHeaders() *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Headers = make(http.Header)
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ResetCookies 重置所有 Cookies
|
|||
|
|
func (r *Request) ResetCookies() *Request {
|
|||
|
|
if r.err != nil {
|
|||
|
|
return r
|
|||
|
|
}
|
|||
|
|
r.config.Cookies = []*http.Cookie{}
|
|||
|
|
return r
|
|||
|
|
}
|