代码拉取完成,页面将自动刷新
package http
import (
"encoding/json"
"net/http"
"strings"
)
// NewResponse 新建回应对象
func NewResponse() *Response {
return &Response{}
}
// ResponseError 响应错误对象
type ResponseError []error
// Error 实现 error 接口
func (e ResponseError) Error() string {
errs := []error(e)
s := []string{}
for _, e := range errs {
s = append(s, e.Error())
}
return strings.Join(s, "\n")
}
// HasErr 是否有错误
func (e ResponseError) HasErr() bool {
if len(e) == 0 {
return false
}
return true
}
// Add 增加错误
func (e ResponseError) Add(err error) ResponseError {
e = append(e, err)
return e
}
// Response 回应对象
type Response struct {
Request *Request
Raw *http.Response
Body []byte
Errs ResponseError
}
// Err 获取响应错误
func (r *Response) Err() error {
if r.Errs.HasErr() {
return r.Errs
}
return nil
}
// JSON 根据json绑定结构体
func (r *Response) JSON(v interface{}) error {
return json.Unmarshal(r.Body, v)
}
// String 获取响应字符串
func (r *Response) String() string {
return string(r.Body)
}
// Byte 获取响应字节
func (r *Response) Byte() []byte {
return r.Body
}
// Status 获取响应状态码
func (r *Response) Status() int {
if r.Raw != nil {
return r.Raw.StatusCode
}
return 0
}
// Header 获取响应header
func (r *Response) Header() http.Header {
if r.Raw != nil {
return r.Raw.Header
}
return nil
}
// Cookies 获取响应 cookie
func (r *Response) Cookies() []*http.Cookie {
if r.Raw != nil {
return r.Raw.Cookies()
}
return nil
}
// IsError 是否响应错误
func (r *Response) IsError() bool {
return r.Raw.StatusCode >= 400 && r.Err() != nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。