requests

package module
v0.0.0-...-8528dc3 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

README

license

原项目 : github.com/asmcos/requests

由于原作者基本不维护. fork项目后, 独立维护的requests项目

requests

Requests is an HTTP library , it is easy to use. Similar to Python requests.

Installation

go get -u github.com/Genesis-Palace/requests

Start

package main

import "github.com/Genesis-Palace/requests"

func main (){

        resp,err := requests.Get("http://go.xiulian.net.cn")
        if err != nil{
          return
        }
        println(resp.Text())
}

Post

package main

import "github.com/Genesis-Palace/requests"


func main (){

        data := requests.Datas{
          "name":"requests_post_test",
        }
        resp,_ := requests.Post("https://www.httpbin.org/post",data)
        println(resp.Text())
}

 Server return data...
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "requests_post_test"
  },
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "23",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "www.httpbin.org",
    "User-Agent": "Go-Requests 0.5"
  },
  "json": null,
  "origin": "114.242.34.110",
  "url": "https://www.httpbin.org/post"
}

Feature Support

  • Set headers
  • Set params
  • Multipart File Uploads
  • Sessions with Cookie Persistence
  • Proxy
  • Authentication
  • JSON
  • Chunked Requests
  • Debug
  • SetTimeout

Set header

example 1
req := requests.Requests()

resp,err := req.Get("http://go.xiulian.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"})
if (err == nil){
  println(resp.Text())
}
example 2
req := requests.Requests()
req.Header.Set("accept-encoding", "gzip, deflate, br")
resp,_ := req.Get("http://go.xiulian.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"})
println(resp.Text())

example 3
h := requests.Header{
  "Referer":         "http://www.jeapedu.com",
  "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
}
resp,_ := req.Get("http://go.xiulian.net.cn",h)

h2 := requests.Header{
  ...
  ...
}
h3,h4 ....
// two or more headers ...
resp,_ = req.Get("http://go.xiulian.net.cn",h,h2,h3,h4)

Set params

p := requests.Params{
  "title": "The blog",
  "name":  "file",
  "id":    "12345",
}
resp,_ := req.Get("http://www.cpython.org", p)

Auth

Test with the correct user information.

req := requests.Requests()
resp,_ := req.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."})
println(resp.Text())

github return

{"login":"asmcos","id":xxxxx,"node_id":"Mxxxxxxxxx==".....

JSON

req := requests.Requests()
req.Header.Set("Content-Type","application/json")
resp,_ = req.Get("https://httpbin.org/json")

var json map[string]interface{}
resp.Json(&json)

for k,v := range json{
  fmt.Println(k,v)
}

SetTimeout

req := Requests()
req.Debug = 1

// 20 Second
req.SetTimeout(20)
req.Get("http://golang.org")

Get Cookies

resp,_ = req.Get("https://www.httpbin.org")
coo := resp.Cookies()
// coo is [] *http.Cookies
println("********cookies*******")
for _, c:= range coo{
  fmt.Println(c.Name,c.Value)
}
modify(在原有基础上进行一定的优化):
  • 支持postJson(合并于原pull request代码)
  • 修改post时header中ContentType默认为form

20200924

postJson支持二进制传输方式

项目会持续维护(by 2019.10.11 17:40:00)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var VERSION string = "0.6"

Functions

This section is empty.

Types

type Auth

type Auth []string

{username,password}

type Datas

type Datas map[string]string // for post form

type Files

type Files map[string]string // name ,filename
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
	Cookies []*http.Cookie
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest() *Request

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)

func (*Request) Post

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

func (*Request) PostJson

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

func (*Request) Proxy

func (req *Request) Proxy(proxyurl string)

func (*Request) RequestDebug

func (req *Request) RequestDebug()

func (*Request) SetCookie

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

cookies cookies only save to Client.Jar req.Cookies is temporary

func (*Request) SetTimeout

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

set timeout s = second

type Response

type Response struct {
	R *http.Response
	// 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)

*************post************************ call req.Post ,only for easy

func (*Response) Content

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

func (*Response) Cookies

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

func (*Response) Json

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

func (*Response) ResponseDebug

func (resp *Response) ResponseDebug()

************

func (*Response) SaveFile

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

func (*Response) Text

func (resp *Response) Text() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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