2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-06-08 20:21:35 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2021-09-30 14:06:46 +08:00
|
|
|
"context"
|
2020-06-26 13:59:33 +08:00
|
|
|
"fmt"
|
2020-06-08 20:21:35 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/net/ghttp"
|
2020-08-24 23:05:30 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
2020-06-26 13:59:33 +08:00
|
|
|
"time"
|
2020-06-08 20:21:35 +08:00
|
|
|
)
|
|
|
|
|
2020-08-24 23:05:30 +08:00
|
|
|
func ExampleHelloWorld() {
|
2020-06-08 20:21:35 +08:00
|
|
|
s := g.Server()
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("hello world")
|
|
|
|
})
|
|
|
|
s.SetPort(8999)
|
|
|
|
s.Run()
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:05:30 +08:00
|
|
|
// Custom saving file name.
|
|
|
|
func ExampleUploadFile_Save() {
|
|
|
|
s := g.Server()
|
|
|
|
s.BindHandler("/upload", func(r *ghttp.Request) {
|
|
|
|
file := r.GetUploadFile("TestFile")
|
|
|
|
if file == nil {
|
|
|
|
r.Response.Write("empty file")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file.Filename = "MyCustomFileName.txt"
|
|
|
|
fileName, err := file.Save(gfile.TempDir())
|
|
|
|
if err != nil {
|
|
|
|
r.Response.Write(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Response.Write(fileName)
|
|
|
|
})
|
|
|
|
s.SetPort(8999)
|
|
|
|
s.Run()
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
func ExampleClientResponse_RawDump() {
|
2021-09-30 14:06:46 +08:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
response, err := g.Client().Get(ctx, "https://goframe.org")
|
2020-06-08 20:21:35 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
response.RawDump()
|
|
|
|
}
|
2020-06-26 13:59:33 +08:00
|
|
|
|
|
|
|
// ExampleClient_SetProxy a example for `ghttp.Client.SetProxy` method.
|
|
|
|
// please prepare two proxy server before running this example.
|
|
|
|
// http proxy server listening on `127.0.0.1:1081`
|
|
|
|
// socks5 proxy server listening on `127.0.0.1:1080`
|
|
|
|
func ExampleClient_SetProxy() {
|
|
|
|
// connect to a http proxy server
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2020-06-26 13:59:33 +08:00
|
|
|
client.SetProxy("http://127.0.0.1:1081")
|
|
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
2021-09-30 14:06:46 +08:00
|
|
|
response, err := client.Get(ctx, "https://api.ip.sb/ip")
|
2020-06-26 13:59:33 +08:00
|
|
|
if err != nil {
|
|
|
|
// err is not nil when your proxy server is down.
|
|
|
|
// eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
response.RawDump()
|
|
|
|
// connect to a http proxy server which needs auth
|
|
|
|
client.SetProxy("http://user:password:127.0.0.1:1081")
|
|
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
2021-09-30 14:06:46 +08:00
|
|
|
response, err = client.Get(ctx, "https://api.ip.sb/ip")
|
2020-06-26 13:59:33 +08:00
|
|
|
if err != nil {
|
|
|
|
// err is not nil when your proxy server is down.
|
|
|
|
// eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
response.RawDump()
|
|
|
|
|
|
|
|
// connect to a socks5 proxy server
|
|
|
|
client.SetProxy("socks5://127.0.0.1:1080")
|
|
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
2021-09-30 14:06:46 +08:00
|
|
|
response, err = client.Get(ctx, "https://api.ip.sb/ip")
|
2020-06-26 13:59:33 +08:00
|
|
|
if err != nil {
|
|
|
|
// err is not nil when your proxy server is down.
|
|
|
|
// eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println(response.RawResponse())
|
|
|
|
|
|
|
|
// connect to a socks5 proxy server which needs auth
|
|
|
|
client.SetProxy("socks5://user:password@127.0.0.1:1080")
|
|
|
|
client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout
|
2021-09-30 14:06:46 +08:00
|
|
|
response, err = client.Get(ctx, "https://api.ip.sb/ip")
|
2020-06-26 13:59:33 +08:00
|
|
|
if err != nil {
|
|
|
|
// err is not nil when your proxy server is down.
|
|
|
|
// eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println(response.RawResponse())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExampleClientChain_Proxy a chain version of example for `ghttp.Client.Proxy` method.
|
|
|
|
// please prepare two proxy server before running this example.
|
|
|
|
// http proxy server listening on `127.0.0.1:1081`
|
|
|
|
// socks5 proxy server listening on `127.0.0.1:1080`
|
|
|
|
// for more details, please refer to ExampleClient_SetProxy
|
|
|
|
func ExampleClientChain_Proxy() {
|
2021-09-30 14:06:46 +08:00
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2021-09-30 14:06:46 +08:00
|
|
|
response, err := client.Proxy("http://127.0.0.1:1081").Get(ctx, "https://api.ip.sb/ip")
|
2020-06-26 13:59:33 +08:00
|
|
|
if err != nil {
|
|
|
|
// err is not nil when your proxy server is down.
|
|
|
|
// eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println(response.RawResponse())
|
|
|
|
|
2020-11-25 16:40:45 +08:00
|
|
|
client2 := g.Client()
|
2021-09-30 14:06:46 +08:00
|
|
|
response, err = client2.Proxy("socks5://127.0.0.1:1080").Get(ctx, "https://api.ip.sb/ip")
|
2020-06-26 13:59:33 +08:00
|
|
|
if err != nil {
|
|
|
|
// err is not nil when your proxy server is down.
|
|
|
|
// eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println(response.RawResponse())
|
|
|
|
}
|