mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
add docs test
This commit is contained in:
227
README.md
227
README.md
@@ -18,11 +18,12 @@ Requests is a fully featured HTTP client library for Golang. Network requests ca
|
||||
---
|
||||
## Features
|
||||
* GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.
|
||||
* Simple for settings and request
|
||||
* [Request](https://github.com/gospider007/requests#Request) Body can be `string`, `[]byte`, `struct`, `map`, `slice` and `io.Reader` too
|
||||
* [Simple for settings and request](https://github.com/gospider007/requests#quickly-send-requests)
|
||||
* [Request](https://pkg.go.dev/github.com/gospider007/requests#RequestOption) Body can be `string`, `[]byte`, `struct`, `map`, `slice` and `io.Reader` too
|
||||
* Auto detects `Content-Type`
|
||||
* Buffer less processing for `io.Reader`
|
||||
* [Response](https://github.com/gospider007/requests#Response) object gives you more possibility
|
||||
* Response object gives you more possibility
|
||||
* [Return whether to reuse connections](https://github.com/gospider007/requests/blob/master/test/isNewConn_test.go)
|
||||
* Automatic marshal and unmarshal for content
|
||||
* Easy to upload one or more file(s) via `multipart/form-data`
|
||||
* Auto detects file content type
|
||||
@@ -35,21 +36,23 @@ Requests is a fully featured HTTP client library for Golang. Network requests ca
|
||||
* goroutine concurrent safe
|
||||
* Gzip - Go does it automatically also requests has fallback handling too
|
||||
* Works fine with `HTTP/2` and `HTTP/1.1`
|
||||
* DNS caching
|
||||
* Fingerprint
|
||||
* [session](https://github.com/gospider007/requests/blob/master/test/session_test.go)
|
||||
* [IPv4, IPv6 Address Control Parsing](https://github.com/gospider007/requests/blob/master/test/addType_test.go)
|
||||
* [DNS Settings](https://github.com/gospider007/requests/blob/master/test/dns_test.go)
|
||||
* [Fingerprint](https://github.com/gospider007/requests/blob/master/test/ja3_test.go)
|
||||
* JA3
|
||||
* HTTP2
|
||||
* JA4
|
||||
* OrderHeaders
|
||||
* Request header capitalization
|
||||
* Proxy
|
||||
* [Proxy](https://github.com/gospider007/requests/blob/master/test/proxy_test.go)
|
||||
* HTTP
|
||||
* HTTPS
|
||||
* SOCKS5
|
||||
* Protocol
|
||||
* HTTP
|
||||
* HTTPS
|
||||
* WebSocket
|
||||
* [WebSocket](https://github.com/gospider007/requests/blob/master/test/websocket_test.go)
|
||||
* SSE
|
||||
* Well tested client library
|
||||
|
||||
@@ -80,15 +83,7 @@ import (
|
||||
|
||||
func main() {
|
||||
href := "http://httpbin.org/anything"
|
||||
resp, err := requests.Post(nil, href, requests.RequestOption{
|
||||
Ja3: true, //enable ja3 fingerprint
|
||||
Cookies: "a=1&b=2", //set cookies
|
||||
// Proxy: "http://127.0.0.1:8888", //set proxy
|
||||
Params: map[string]any{"query": "cat"}, //set query params
|
||||
Headers: map[string]any{"token": 12345}, //set headers
|
||||
Json: map[string]any{"age": 60}, //send json data
|
||||
Timeout: time.Second * 10, //set timeout
|
||||
})
|
||||
resp, err := requests.Get(nil, "http://httpbin.org/anything")
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
@@ -99,206 +94,6 @@ func main() {
|
||||
log.Print(resp.Cookies()) // Get cookies
|
||||
}
|
||||
```
|
||||
### use session
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
href := "http://httpbin.org/anything"
|
||||
session, err := requests.NewClient(nil) //use session
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
resp, err := session.Get(nil, href)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Print(resp.StatusCode()) //return status code
|
||||
}
|
||||
```
|
||||
### setting order headers with http1
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resp, err := requests.Get(nil, "http://httpbin.org/anything", requests.RequestOption{
|
||||
OrderHeaders: []string{"accept-encoding"}},
|
||||
)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Print(resp.Text())
|
||||
}
|
||||
```
|
||||
|
||||
### send websocket
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
"github.com/gospider007/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
response, err := requests.Get(nil, "ws://82.157.123.54:9010/ajaxchattest", requests.RequestOption{Headers: map[string]string{
|
||||
"Origin": "http://coolaf.com",
|
||||
}}) // Send WebSocket request
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
defer response.Close()
|
||||
wsCli := response.WebSocket()
|
||||
if err = wsCli.Send(nil, websocket.MessageText, "test"); err != nil { // Send text message
|
||||
log.Panic(err)
|
||||
}
|
||||
msgType, con, err := wsCli.Recv(nil) // Receive message
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Print(msgType) // Message type
|
||||
log.Print(string(con)) // Message content
|
||||
}
|
||||
```
|
||||
### IPv4, IPv6 Address Control Parsing
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
session, _ := requests.NewClient(nil, requests.ClientOption{
|
||||
AddrType: requests.Ipv4, // Prioritize parsing IPv4 addresses
|
||||
// AddrType: requests.Ipv6, // Prioritize parsing IPv6 addresses
|
||||
})
|
||||
resp, err := session.Get(nil, "https://test.ipw.cn")
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Print(resp.Text())
|
||||
log.Print(resp.StatusCode())
|
||||
}
|
||||
```
|
||||
### Generate Ja3 Fingerprint from String
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/ja3"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ja3Str := "772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513,29-23-24,0"
|
||||
Ja3Spec, _ := ja3.CreateSpecWithStr(ja3Str) // Generate fingerprint from string
|
||||
resp, err := requests.Get(nil, "https://tools.scrapfly.io/api/fp/ja3?extended=1", requests.RequestOption{Ja3Spec: Ja3Spec})
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
jsonData, _ := resp.Json()
|
||||
log.Print(jsonData.Get("ja3").String())
|
||||
log.Print(jsonData.Get("ja3").String() == ja3Str)
|
||||
}
|
||||
```
|
||||
### Generate Ja3 Fingerprint from ID
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/ja3"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
Ja3Spec, _ := ja3.CreateSpecWithId(ja3.HelloChrome_Auto) // Generate fingerprint from ID
|
||||
resp, err := requests.Get(nil, "https://tools.scrapfly.io/api/fp/ja3?extended=1", requests.RequestOption{Ja3Spec: Ja3Spec})
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
jsonData, _ := resp.Json()
|
||||
log.Print(jsonData.Get("ja3").String())
|
||||
}
|
||||
```
|
||||
### Generate H2 Fingerprint from String
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/ja3"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
h2ja3Str := "1:65536,2:0,4:6291456,6:262144|15663105|0|m,a,s,p"
|
||||
h2ja3Spec, _ := ja3.CreateH2SpecWithStr(h2ja3Str) // Generate fingerprint from string
|
||||
resp, err := requests.Get(nil, "https://tools.scrapfly.io/api/fp/akamai", requests.RequestOption{H2Ja3Spec: h2ja3Spec})
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
jsonData, _ := resp.Json()
|
||||
log.Print(jsonData.Get("akamai_fp").String())
|
||||
log.Print(jsonData.Get("akamai_fp").String() == h2ja3Str)
|
||||
}
|
||||
```
|
||||
### Modify H2 Fingerprint
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gospider007/ja3"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func main() {
|
||||
h2ja3Spec := ja3.H2Ja3Spec{
|
||||
InitialSetting: []ja3.Setting{
|
||||
{Id: 1, Val: 65555},
|
||||
{Id: 2, Val: 1},
|
||||
{Id: 3, Val: 2000},
|
||||
{Id: 4, Val: 6291457},
|
||||
{Id: 6, Val: 262145},
|
||||
},
|
||||
ConnFlow: 15663106,
|
||||
OrderHeaders: []string{
|
||||
":method",
|
||||
":path",
|
||||
":scheme",
|
||||
":authority",
|
||||
},
|
||||
}
|
||||
resp, err := requests.Get(nil, "https://tools.scrapfly.io/api/fp/anything", requests.RequestOption{H2Ja3Spec: h2ja3Spec})
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Print(resp.Text())
|
||||
}
|
||||
```
|
||||
|
||||
# Contributing
|
||||
If you have a bug report or feature request, you can [open an issue](../../issues/new)
|
||||
|
||||
21
test/addType_test.go
Normal file
21
test/addType_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/gtls"
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestAddType(t *testing.T) {
|
||||
session, _ := requests.NewClient(nil, requests.ClientOption{
|
||||
AddrType: gtls.Ipv4, // Prioritize parsing IPv4 addresses
|
||||
})
|
||||
resp, err := session.Get(nil, "https://test.ipw.cn")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Errorf("status code error, expected 200, got %d", resp.StatusCode())
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,8 @@ import (
|
||||
)
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
resp, err := requests.Get(nil, "https://myip.top", requests.RequestOption{
|
||||
Dns: &net.UDPAddr{
|
||||
// IP: net.ParseIP("223.6.6.6"),
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Dns: &net.UDPAddr{ //set dns server
|
||||
IP: net.ParseIP("223.5.5.5"),
|
||||
Port: 53,
|
||||
},
|
||||
|
||||
25
test/isNewConn_test.go
Normal file
25
test/isNewConn_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestDefaultClient(t *testing.T) {
|
||||
for i := 0; i < 2; i++ {
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if i == 0 {
|
||||
if !resp.IsNewConn() { //return is NewConn
|
||||
t.Error("new conn error")
|
||||
}
|
||||
} else {
|
||||
if resp.IsNewConn() {
|
||||
t.Error("new conn error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,7 @@ import (
|
||||
|
||||
func TestJa3(t *testing.T) {
|
||||
j := "772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,5-27-13-35-16-18-43-17513-65281-51-45-11-0-10-23,12092-29-23-24,0"
|
||||
// j := "772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,5-27-13-35-16-18-43-17513-65281-51-45-11-0-10-23-41,12092-29-23-24,0"
|
||||
ja3Spec, err := ja3.CreateSpecWithStr(j)
|
||||
ja3Spec, err := ja3.CreateSpecWithStr(j) //create ja3 spec with string
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -23,8 +22,8 @@ func TestJa3(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
ja3 := jsonData.Get("ja3.ja3")
|
||||
jsonData, err := resp.Json() //parse json
|
||||
ja3 := jsonData.Get("ja3.ja3") //get ja3 value
|
||||
if ja3 == nil {
|
||||
t.Fatal("not found ja3")
|
||||
}
|
||||
@@ -37,14 +36,14 @@ func TestJa3(t *testing.T) {
|
||||
func TestJa3Psk(t *testing.T) {
|
||||
j := "772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,5-27-13-35-16-18-43-17513-65281-51-45-11-0-10-23-41,12092-29-23-24,0"
|
||||
j2 := "772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,5-27-13-35-16-18-43-17513-65281-51-45-11-0-10-23,12092-29-23-24,0"
|
||||
ja3Spec, err := ja3.CreateSpecWithStr(j)
|
||||
ja3Spec, err := ja3.CreateSpecWithStr(j) //create ja3 spec with string
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, _ := requests.NewClient(nil)
|
||||
for i := 0; i < 2; i++ {
|
||||
resp, err := session.Get(nil, "https://tools.scrapfly.io/api/fp/anything", requests.RequestOption{
|
||||
Ja3Spec: ja3Spec,
|
||||
Ja3Spec: ja3Spec, //set ja3 spec
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -70,12 +69,12 @@ func TestJa3Psk(t *testing.T) {
|
||||
|
||||
func TestH2(t *testing.T) {
|
||||
j := "1:65536,2:0,4:6291456,6:262144|15663105|0|m,a,s,p"
|
||||
h2Spec, err := ja3.CreateH2SpecWithStr(j)
|
||||
h2Spec, err := ja3.CreateH2SpecWithStr(j) //create h2 spec with string
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := requests.Get(nil, "https://tools.scrapfly.io/api/fp/anything", requests.RequestOption{
|
||||
H2Ja3Spec: h2Spec,
|
||||
H2Ja3Spec: h2Spec, //set h2 spec
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -103,7 +102,7 @@ func TestOrderHeaders(t *testing.T) {
|
||||
"User-Agent",
|
||||
}
|
||||
resp, err := requests.Get(nil, "https://tools.scrapfly.io/api/fp/anything", requests.RequestOption{
|
||||
OrderHeaders: orderHeaders,
|
||||
OrderHeaders: orderHeaders, //set http1.1 order headers
|
||||
ForceHttp1: true,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
36
test/proxy_test.go
Normal file
36
test/proxy_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestProxy(t *testing.T) {
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Proxy: "", //set proxy,ex:"http://127.0.0.1:8080","https://127.0.0.1:8080","socks5://127.0.0.1:8080"
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Error("status code is not 200")
|
||||
}
|
||||
}
|
||||
func TestGetProxy(t *testing.T) {
|
||||
session, _ := requests.NewClient(nil, requests.ClientOption{
|
||||
GetProxy: func(ctx context.Context, url *url.URL) (string, error) { //Penalty when creating a new connection
|
||||
proxy := "" //set proxy,ex:"http://127.0.0.1:8080","https://127.0.0.1:8080","socks5://127.0.0.1:8080"
|
||||
return proxy, nil
|
||||
},
|
||||
})
|
||||
resp, err := session.Get(nil, "https://httpbin.org/anything")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Error("status code is not 200")
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestDefaultClient(t *testing.T) {
|
||||
for i := 0; i < 2; i++ {
|
||||
resp, err := requests.Get(nil, "https://myip.top")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
log.Printf("send num: %d, new conn: %v", i, resp.IsNewConn())
|
||||
}
|
||||
}
|
||||
}
|
||||
26
test/session_test.go
Normal file
26
test/session_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestSession(t *testing.T) {
|
||||
session, _ := requests.NewClient(nil)
|
||||
for i := 0; i < 2; i++ {
|
||||
resp, err := session.Get(nil, "https://httpbin.org/anything")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if i == 0 {
|
||||
if !resp.IsNewConn() { //return is NewConn
|
||||
t.Error("new conn error")
|
||||
}
|
||||
} else {
|
||||
if resp.IsNewConn() {
|
||||
t.Error("new conn error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
test/websocket_test.go
Normal file
32
test/websocket_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
"github.com/gospider007/websocket"
|
||||
)
|
||||
|
||||
func TestWebSocket(t *testing.T) {
|
||||
response, err := requests.Get(nil, "ws://82.157.123.54:9010/ajaxchattest", requests.RequestOption{Headers: map[string]string{
|
||||
"Origin": "http://coolaf.com",
|
||||
}}) // Send WebSocket request
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer response.CloseBody()
|
||||
wsCli := response.WebSocket()
|
||||
if err = wsCli.Send(nil, websocket.MessageText, "test"); err != nil { // Send text message
|
||||
t.Error(err)
|
||||
}
|
||||
msgType, con, err := wsCli.Recv(nil) // Receive message
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if msgType != websocket.MessageText {
|
||||
t.Error("Message type is not text")
|
||||
}
|
||||
if string(con) != "test" {
|
||||
t.Error("Message content is not test")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user