You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
simpleRequest/README.MD

197 lines
4.5 KiB
Plaintext

3 years ago
# simpleRequest
3 years ago
## 1. 说明
3 years ago
[simpleRequest](www.github.com/dorlolo/simpleRequest) 是基于golang原生http库的封装适合用来对第三方接口进行快速地对接和开发。
它具备以下特点:
- 相对于其它请求库,更易于理解和使用。极大减少了开发过程中的代码量和资料查询时间。
- 适合对接一些未遵循restful规范的接口。
3 years ago
3 years ago
## 2. 如何使用?
3 years ago
3 years ago
### 2.1 模块导入
3 years ago
```go
3 years ago
import "github.com/dorlolo/simpleRequest"
3 years ago
```
3 years ago
### 2.2 实例化
3 years ago
```go
3 years ago
var r = simpleRequest.NewRequest()
3 years ago
```
3 years ago
### 2.3 添加请求头
3 years ago
3 years ago
#### 2.3.1 单个赋值
3 years ago
```go
r.Headers().Set("token", "d+jfdji*D%1=")
r.Headers().Set("Content-Type", "application/json")
```
3 years ago
#### 2.3.2 map赋值
3 years ago
```go
mapHeaders:= map[string]string{
"token": "d+jfdji*D%1=",
"Content-Type": "application/json",
}
r.Headers().Sets(mapHeaders)
```
3 years ago
#### 2.3.3 链式赋值
3 years ago
```go
r.Headers().Set("token", "d+jfdji*D%1=").Set("Content-Type", "application/json")
```
3 years ago
3 years ago
#### 2.3.4 添加多值
3 years ago
对单个key添加多只不要使用`.set`,因为原先的值会被覆盖
```go
r.Headers().Set("Accept", "text/html")
r.Headers().Add("Accept","application/xhtml+xml")
r.Headers().Add("Accept","application/xml;q=0.8")
r.Headers().Add("Accept","image/webp")
r.Headers().Add("Accept","*/*;q=0.8")
```
3 years ago
#### 2.3.4 使用预设的key
3 years ago
```go
3 years ago
r.Headers().SetConentType("application/json")
//r.Headers().Set("Content-Type", "application/json")
3 years ago
r.Headers().SetUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1"")
//r.Headers().Set("User-Agent",Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1")
3 years ago
r.Headers().SetConentEncoding("gzip, deflate, br")
//r.Headers().Set("Content-Encoding", "gzip, deflate, br")
3 years ago
```
3 years ago
3 years ago
#### 2.3.5 使用预设的key-value
3 years ago
```go
//随机user-agent
r.Headers().SetRandomUerAgent()
//r.Headers().Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1")
r.Headers().ConentType_json()
//r.Headers().Set("Content-Type", "application/json")
r.Headers().ConentType_formData()
3 years ago
//r.Headers().Set("Content-Type","multipart/form-data")
r.Headers().ConentType_formUrlencoded()
3 years ago
//r.Headers().Set("Content-Type","application/x-www-form-urlencoded")
r.Headers().ConentType_textPlain()
3 years ago
//r.Headers().Set("Content-Type","text/plain; charset=utf-8")
```
3 years ago
### 2.4 添加queryParams
#### 2.4.1 单个赋值
3 years ago
```go
r.QueryParams().Set("user", "dorlolo")
```
3 years ago
#### 2.4.2 map赋值
3 years ago
不会覆盖上面之前填充过的参数
```go
pamarsBulid := make(map[string]interface{})
pamarsBulid["passwd"] = "123456"
pamarsBulid["action"] = "login"
r.QueryParams().Sets(pamarsBulid)
```
3 years ago
#### 2.4.3 链式赋值
3 years ago
```go
r.QueryParams().Set("user", "dorlolo").Set("passwd","123456")
```
3 years ago
#### 2.4.4 获取url.Values对象进行赋值
3 years ago
对象类型为`*url.Values`,取到地址后,可以使用`url.Values`中的方法继续进行赋值
```go
qpData:=r.QueryParams().Gets()
qpData.Add("age","18")
```
3 years ago
### 2.5 添加请求体body
3 years ago
3 years ago
#### 2.5.1 单个赋值
3 years ago
```go
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
```
3 years ago
#### 2.5.2 map赋值
3 years ago
```go
3 years ago
bodyBulid := map[string]interface{}{
3 years ago
"beginDate":"2022-03-01",
"endDate":"2022-03-03",
}
r.Body().Sets(bodyBulid)
```
3 years ago
#### 2.5.3 链式赋值
3 years ago
```go
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
```
3 years ago
#### 2.5.4 字符串赋值
3 years ago
json格式不要使用此方法
3 years ago
```go
bodydata:=`{"devSn":"230000000008","type":"day"}`
r.Body().SetString(bodydata)
```
3 years ago
### 2.6 其它请求参数
3 years ago
3 years ago
#### 2.6.1 设置超时时间
3 years ago
```go
r.TimeOut(time.Second * 30)
```
3 years ago
#### 2.6.2 跳过证书验证
3 years ago
```go
r.SkipCertVerify()
```
3 years ago
### 2.7 发送请求
#### 2.7.1 post请求
3 years ago
```go
res, err :=r.Post("https://127.0.0.1:80/excample")
if err != nil {
ftm.Println( "error occured", err)
} else {
fmt.Println(res)
}
```
#### 2.7.2 发送请求
3 years ago
```go
res, err :=r.Get("https://127.0.0.1:80/excample")
if err != nil {
ftm.Println( "error occured", err)
} else {
fmt.Println(res)
}
```
**支持的请求类型有**
- POST
- GET
- PUT
- DELETE
- PATCH
- HEAD
- CONNECT
- OPTIONS
- TRACE
3 years ago
3 years ago
### 2.8 获取上下文
3 years ago
请注意,需要完成请求后才能获得上下文数据!
3 years ago
#### 2.8.1 获取请求的上下文对象
3 years ago
```go
requestContext:=r.Request
```
3 years ago
#### 2.8.2 获取返回的上下文对象
3 years ago
```go
responseContext:=r.Response
```
3 years ago
## 3. 使用示例
[simpleRequest_test.go](excample/simpleRequest_test.go)