requests

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

README

requests

一个类似 Python requests 的 Go HTTP 请求库

使用go get安装

go get -u github.com/losenli/requests

简单的GET请求

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main (){
	resp,err := requests.Get("https://www.httpbin.org/get")
	if err != nil{
		log.Fatalln(err)
	}
	log.Println(resp.Text())
}

传递 URL 参数

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main() {
	payload := requests.Params{"key1": "value1", "key2": "value2"}
	resp, _ := requests.Get("http://www.httpbin.org/get", payload)
	log.Println(resp.Text())
}

响应内容

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main() {
	resp, _ := requests.Get("http://www.httpbin.org/get")
	log.Println(resp.Text())
}

二进制响应内容

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main() {
	resp, _ := requests.Get("http://www.httpbin.org/get")
	log.Println(resp.Content())
}

JSON 响应内容

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main() {
	resp, _ := requests.Get("http://www.httpbin.org/get")
	log.Println(resp.Json())
}

引入gjson.Result

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main() {
	resp, _ := requests.Get("http://www.httpbin.org/get")
	log.Println(resp.Result())
	log.Println(resp.Result().Get("headers.User-Agent"))
}

Post请求

package main

import (
	"github.com/losenli/requests"
	"log"
)

func main() {
	data := requests.DataItem{
		"name": "requests_post_test",
	}
	resp, _ := requests.Post("https://www.httpbin.org/post", data)
	log.Println(resp.Text())
}

定制请求头

传递 json 数据的 POST 请求

package main

import (
	"github.com/losenli/requests"
)

func main (){
	jsonStr := "{\"name\":\"requests_post_test\"}"
	resp,_ := requests.PostJson("http://www.httpbin.org/post",jsonStr)
	println(resp.Text())
}

Documentation

Index

Constants

View Source
const (
	TypeJSON       = "application/json"
	TypeXML        = "application/xml"
	TypeForm       = "application/x-www-form-urlencoded"
	TypeFormData   = "application/x-www-form-urlencoded"
	TypeUrlencoded = "application/x-www-form-urlencoded"
	TypeHTML       = "text/html"
	TypeText       = "text/plain"
	TypeMultipart  = "multipart/form-data"
)

支持的请求类型

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth []string

type DataItem

type DataItem map[string]string

type Files

type Files map[string]string
type Header map[string]string

自定义类型

type Params

type Params map[string]string

type Request

type Request struct {
	Header  *http.Header
	Client  *http.Client
	Debug   int
	Host    string
	Cookies []*http.Cookie
	// contains filtered or unexported fields
}

封装请求结构体

func Requests

func Requests() *Request

func (*Request) ClearCookies

func (req *Request) ClearCookies()

func (*Request) ClientSetCookies

func (req *Request) ClientSetCookies()

func (*Request) Get

func (req *Request) Get(origUrl string, args ...interface{}) (resp *Response, err error)

构造 GET 请求

func (*Request) Post

func (req *Request) Post(origUrl string, args ...interface{}) (resp *Response, err error)

构造 POST 请求

func (*Request) PostJson

func (req *Request) PostJson(origUrl string, args ...interface{}) (resp *Response, err error)

发送JSON请求

func (*Request) Proxy

func (req *Request) Proxy(proxyUrl string)

func (*Request) RequestDebug

func (req *Request) RequestDebug()

func (*Request) Send

func (req *Request) Send(method, origUrl string, args ...interface{}) (resp *Response, err error)

发送HTTP请求

func (*Request) SetCookie

func (req *Request) SetCookie(cookie *http.Cookie)

func (*Request) SetTimeout

func (req *Request) SetTimeout(n time.Duration)

设置超时时间

type Response

type Response struct {
	R *http.Response

	StatusCode int
	Time       int64
	Length     int64
	// contains filtered or unexported fields
}

封装响应结构体

func Get

func Get(origUrl string, args ...interface{}) (resp *Response, err error)

func Post

func Post(origUrl string, args ...interface{}) (resp *Response, err error)

func PostJson

func PostJson(origUrl string, args ...interface{}) (resp *Response, err error)

func (*Response) Content

func (resp *Response) Content() []byte

返回二进制响应内容

func (*Response) Cookies

func (resp *Response) Cookies() (cookies []*http.Cookie)

获取Cookies

func (*Response) Json

func (resp *Response) Json() (map[string]interface{}, error)

默认使用Map反序列化响应

func (*Response) ResponseDebug

func (resp *Response) ResponseDebug()

func (*Response) Result

func (resp *Response) Result() gjson.Result

转换成gjson.Result,方便解析JSON响应

func (*Response) SaveFile

func (resp *Response) SaveFile(filename string) error

响应内容保存到文件

func (*Response) Text

func (resp *Response) Text() string

返回字符串响应内容

func (*Response) Unmarshal

func (resp *Response) Unmarshal(v interface{}) error

响应内容反序列化

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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