1 Star 0 Fork 0

王南北丶/direwolf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api.go 3.15 KB
一键复制 编辑 原始数据 按行查看 历史
王南北丶 提交于 2019-12-27 17:34 . add response method: Json, JsonGet
/*
Package direwolf is a convenient and easy to use http client written in Golang.
*/
package direwolf
// Default global session
var defatultSession *Session
func init() {
sessionOptions := DefaultSessionOptions() // New default global session
sessionOptions.DisableCookieJar = true
defatultSession = NewSession(sessionOptions)
}
// Send is different with Get and Post method, you should pass a
// Request to it. You can construct Request by use NewRequest
// method.
func Send(req *Request) (*Response, error) {
resp, err := defatultSession.Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Get is the most common method of direwolf to constructs and sends a
// Get request.
//
// You can construct this request by passing the following parameters:
// url: URL for the request.
// http.Header: HTTP Headers to send.
// direwolf.Params: Parameters to send in the query string.
// direwolf.Cookies: Cookies to send.
// direwolf.PostForm: Post data form to send.
// direwolf.Body: Post body to send.
// direwolf.Proxy: Proxy url to use.
// direwolf.Timeout: Request Timeout. Default value is 30.
// direwolf.RedirectNum: Number of Request allowed to redirect. Default value is 5.
func Get(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("GET", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Post is the method to constructs and sends a Post request. Parameters are
// the same with direwolf.Get()
//
// Note: direwolf.Body can`t existed with direwolf.PostForm.
func Post(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("POST", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Head is the method to constructs and sends a Head request. Parameters are
// the same with direwolf.Get()
func Head(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("HEAD", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Put is the method to constructs and sends a Put request. Parameters are
// the same with direwolf.Get()
func Put(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("Put", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Patch is the method to constructs and sends a Patch request. Parameters are
// the same with direwolf.Get()
func Patch(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("Patch", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
// Delete is the method to constructs and sends a Delete request. Parameters are
// the same with direwolf.Get()
func Delete(URL string, args ...RequestOption) (*Response, error) {
req, err := NewRequest("Delete", URL, args...)
if err != nil {
return nil, err
}
resp, err := Send(req)
if err != nil {
return nil, err
}
return resp, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wnanbei/direwolf.git
git@gitee.com:wnanbei/direwolf.git
wnanbei
direwolf
direwolf
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385