req

package module
v0.0.0-...-aa8b059 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 15 Imported by: 0

README

req

像python一样优雅的Go爬虫库

通过对net/http的再一次封装,使得req的语法更接近python requests的写法

一、安装

go get github.com/serialt/req

二、Demo

1.get请求

package main

import (
	"fmt"

	"github.com/serialt/req"
)

func main() {
	resp, err := req.Get("https://www.httpbin.org/get?a=1&b=2")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(resp.Text())

	// Params
	params := req.Params{"a": "1", "b": "2"}
	res, err := req.Get("https://www.httpbin.org/get", params)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(res.Text())
}

2.post请求

package main

import (
	"fmt"

	"github.com/serialt/req"
)

func main() {
	// form post
	data := req.Data{
		"name": "xiaobulv",
	}
	resp, _ := req.Post("https://www.httpbin.org/post", data)
	fmt.Println(resp.Text())

	// json post
	jdata := req.Json{"AGE": "131"}
	respb, _ := req.Post("https://www.httpbin.org/post", jdata)
	fmt.Println(respb.Text())

}

3.put delete patch等

package main

import (
	"fmt"

	"github.com/serialt/req"
)

func main() {
	// put
	putdata := req.Json{"AGE": "131"}
	respp, _ := req.Put("https://www.httpbin.org/put", putdata)
	fmt.Println(respp.Text())
}

4.auth

package main

import (
	"github.com/serialt/req"
)

func main() {
	respa, _ := req.Get("https://www.httpbin.org/basic-auth/admin/123456", req.Auth{"admin", "123456"})
	println(respa.Text())
}

5.header, cookie, timeout, proxy, json等设置

package main

import (
	"fmt"

	"github.com/serialt/req"
)

func main() {
	// 设置header
	header := req.Header{"user-agent": "hello"}

	// post 请求参数
	data := req.Data{
		"name": "xiaobulv",
	}

	// 设置cookie
	// 当Header里有Cookie时, 此设置无效
	ck := req.Cookie{"BIDUPSID": "C855441CA6145FBB2741293580"}

	// timeout
	timeout := req.SetTimeout(10)

	// 代理
	// proxy := req.Proxy("http://xxx.ip.com")

	resp, _ := req.Post("https://www.httpbin.org/post", data, header, ck, timeout)

	// content
	fmt.Println(resp.Content())

	// text
	fmt.Println(resp.Text())

	// response.Json
	m := make(map[string]interface{})
	resp.Json(&m)
	fmt.Println(m["headers"])
	c := m["headers"]
	fmt.Println(c.(map[string]interface{})["Host"])

	// 状态码
	fmt.Println(resp.StatusCode)

	// 响应cookie
	fmt.Println(resp.Cookies())
}

6.tls设置

package main

import (
	"fmt"

	"github.com/serialt/req"
)
func main() {

	// tls双向认证
	// certificate, _ := req.NewCertificateFromFiles("xxx.crt", "xxx.key")
	testConfig := req.TLSConfig{
		InsecureSkipVerify: true,
		// 自签名证书的ca
		ClientCAs:          req.NewCAPoolFromFiles("/tmp/ccc/ca.crt"),
		// Certificates:       []tls.Certificate{certificate},
	}

	resp, err := req.Get("https://example.com", testConfig)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(resp.Text())
	}
}

7.上传文件

package main

import (
	"fmt"

	"github.com/serialt/req"
)

func main() {
	// form post
	files := req.Files{
		"file1": "/tmp/ccc/file1.txt",
	}
	header := req.Header{
		"content-type": "multipart/form-data",
	}
	resp, _ := req.Post("https://www.httpbin.org/post", files)
	fmt.Println(resp.Text())

	// json post
	jdata := req.Json{"AGE": "131"}
	respb, _ := req.Post("https://www.httpbin.org/post", header, jdata)
	fmt.Println(respb.Text())

}

Thanks

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCAPool

func NewCAPool(crts ...[]byte) *x509.CertPool

func NewCAPoolFromFiles

func NewCAPoolFromFiles(files ...string) *x509.CertPool

func NewCertificate

func NewCertificate(certData []byte, keyData []byte) (tls.Certificate, error)

func NewCertificateFromFiles

func NewCertificateFromFiles(certFile string, keyFile string) (tls.Certificate, error)

Types

type Auth

type Auth []string

{username,password}

type Cookie map[string]string

type Data

type Data map[string]string

type Files

type Files map[string]string // name ,filename
type Header map[string]string

type Json

type Json map[string]string

type Params

type Params map[string]string

type Proxy

type Proxy string

type Request

type Request struct {
	Header *http.Header
	Client *http.Client
	// contains filtered or unexported fields
}

func Requests

func Requests() *Request

func (*Request) Close

func (req *Request) Close()

func (*Request) ReqCookies

func (req *Request) ReqCookies() []*http.Cookie

func (*Request) Send

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

type Response

type Response struct {
	Res *http.Response

	Req        *Request
	Status     string // e.g. "200 OK"
	StatusCode int    // e.g. 200
	Header     http.Header
	// contains filtered or unexported fields
}

func Delete

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

func Get

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

常见几种请求类型

func Patch

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

func Post

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

func Put

func Put(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)

func (*Response) EndStruct

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

func (*Response) Json

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

func (*Response) Location

func (resp *Response) Location() (*url.URL, error)

func (*Response) SaveFile

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

func (*Response) Text

func (resp *Response) Text() string

type SetTimeout

type SetTimeout int

type TLSConfig

type TLSConfig struct {
	Certificates       []tls.Certificate
	NameToCertificate  map[string]*tls.Certificate
	RootCAs            *x509.CertPool
	ClientCAs          *x509.CertPool
	InsecureSkipVerify bool
}

Directories

Path Synopsis
* @Description : Blue Planet * @Author : serialt * @Email : tserialt@gmail.com * @Created Time : 2023-04-18 22:02:19 * @Last modified : 2023-10-11 20:12:16 * @FilePath : /req/example/main.go * @Other : * @ : * * *
* @Description : Blue Planet * @Author : serialt * @Email : tserialt@gmail.com * @Created Time : 2023-04-18 22:02:19 * @Last modified : 2023-10-11 20:12:16 * @FilePath : /req/example/main.go * @Other : * @ : * * *

Jump to

Keyboard shortcuts

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