更新md
This commit is contained in:
parent
13195ffe32
commit
bcc1694c2c
64
README.MD
64
README.MD
@ -1,33 +1,33 @@
|
|||||||
# simpleRequest
|
# simpleRequest
|
||||||
## 说明
|
## 1. 说明
|
||||||
[simpleRequest](www.github.com/dorlolo/simpleRequest) 是基于golang原生http库的封装,适合用来对第三方接口进行快速地对接和开发。
|
[simpleRequest](www.github.com/dorlolo/simpleRequest) 是基于golang原生http库的封装,适合用来对第三方接口进行快速地对接和开发。
|
||||||
它具备以下特点:
|
它具备以下特点:
|
||||||
- 相对于其它请求库,更易于理解和使用。极大减少了开发过程中的代码量和资料查询时间。
|
- 相对于其它请求库,更易于理解和使用。极大减少了开发过程中的代码量和资料查询时间。
|
||||||
- 适合对接一些未遵循restful规范的接口。
|
- 适合对接一些未遵循restful规范的接口。
|
||||||
|
|
||||||
## 如何使用?
|
## 2. 如何使用?
|
||||||
|
|
||||||
### 模块导入
|
### 2.1 模块导入
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/dorlolo/simpleRequest"
|
import "github.com/dorlolo/simpleRequest"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 实例化
|
### 2.2 实例化
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var r = simpleRequest.NewRequest()
|
var r = simpleRequest.NewRequest()
|
||||||
```
|
```
|
||||||
|
|
||||||
### 添加请求头
|
### 2.3 添加请求头
|
||||||
|
|
||||||
#### 单个赋值
|
#### 2.3.1 单个赋值
|
||||||
```go
|
```go
|
||||||
r.Headers().Set("token", "d+jfdji*D%1=")
|
r.Headers().Set("token", "d+jfdji*D%1=")
|
||||||
r.Headers().Set("Content-Type", "application/json")
|
r.Headers().Set("Content-Type", "application/json")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### map赋值
|
#### 2.3.2 map赋值
|
||||||
```go
|
```go
|
||||||
mapHeaders:= map[string]string{
|
mapHeaders:= map[string]string{
|
||||||
"token": "d+jfdji*D%1=",
|
"token": "d+jfdji*D%1=",
|
||||||
@ -36,12 +36,12 @@ mapHeaders:= map[string]string{
|
|||||||
r.Headers().Sets(mapHeaders)
|
r.Headers().Sets(mapHeaders)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 链式赋值
|
#### 2.3.3 链式赋值
|
||||||
```go
|
```go
|
||||||
r.Headers().Set("token", "d+jfdji*D%1=").Set("Content-Type", "application/json")
|
r.Headers().Set("token", "d+jfdji*D%1=").Set("Content-Type", "application/json")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 添加多值
|
#### 2.3.4 添加多值
|
||||||
对单个key添加多只不要使用`.set`,因为原先的值会被覆盖
|
对单个key添加多只不要使用`.set`,因为原先的值会被覆盖
|
||||||
```go
|
```go
|
||||||
r.Headers().Set("Accept", "text/html")
|
r.Headers().Set("Accept", "text/html")
|
||||||
@ -51,7 +51,7 @@ r.Headers().Add("Accept","image/webp")
|
|||||||
r.Headers().Add("Accept","*/*;q=0.8")
|
r.Headers().Add("Accept","*/*;q=0.8")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 使用预设的key
|
#### 2.3.4 使用预设的key
|
||||||
```go
|
```go
|
||||||
r.Headers().SetConentType("application/json")
|
r.Headers().SetConentType("application/json")
|
||||||
//r.Headers().Set("Content-Type", "application/json")
|
//r.Headers().Set("Content-Type", "application/json")
|
||||||
@ -63,7 +63,7 @@ r.Headers().SetConentEncoding("gzip, deflate, br")
|
|||||||
//r.Headers().Set("Content-Encoding", "gzip, deflate, br")
|
//r.Headers().Set("Content-Encoding", "gzip, deflate, br")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 使用预设的key-value
|
#### 2.3.5 使用预设的key-value
|
||||||
```go
|
```go
|
||||||
//随机user-agent
|
//随机user-agent
|
||||||
r.Headers().SetRandomUerAgent()
|
r.Headers().SetRandomUerAgent()
|
||||||
@ -82,12 +82,12 @@ r.Headers()ConentType_textPlain()
|
|||||||
//r.Headers().Set("Content-Type","text/plain; charset=utf-8")
|
//r.Headers().Set("Content-Type","text/plain; charset=utf-8")
|
||||||
```
|
```
|
||||||
|
|
||||||
### 添加queryParams
|
### 2.4 添加queryParams
|
||||||
#### 单个赋值
|
#### 2.4.1 单个赋值
|
||||||
```go
|
```go
|
||||||
r.QueryParams().Set("user", "dorlolo")
|
r.QueryParams().Set("user", "dorlolo")
|
||||||
```
|
```
|
||||||
#### map赋值
|
#### 2.4.2 map赋值
|
||||||
不会覆盖上面之前填充过的参数
|
不会覆盖上面之前填充过的参数
|
||||||
```go
|
```go
|
||||||
pamarsBulid := make(map[string]interface{})
|
pamarsBulid := make(map[string]interface{})
|
||||||
@ -96,27 +96,27 @@ pamarsBulid["action"] = "login"
|
|||||||
r.QueryParams().Sets(pamarsBulid)
|
r.QueryParams().Sets(pamarsBulid)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 链式赋值
|
#### 2.4.3 链式赋值
|
||||||
```go
|
```go
|
||||||
r.QueryParams().Set("user", "dorlolo").Set("passwd","123456")
|
r.QueryParams().Set("user", "dorlolo").Set("passwd","123456")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 获取url.Values对象进行赋值
|
#### 2.4.4 获取url.Values对象进行赋值
|
||||||
对象类型为`*url.Values`,取到地址后,可以使用`url.Values`中的方法继续进行赋值
|
对象类型为`*url.Values`,取到地址后,可以使用`url.Values`中的方法继续进行赋值
|
||||||
```go
|
```go
|
||||||
qpData:=r.QueryParams().Gets()
|
qpData:=r.QueryParams().Gets()
|
||||||
qpData.Add("age","18")
|
qpData.Add("age","18")
|
||||||
```
|
```
|
||||||
|
|
||||||
### 添加请求体body
|
### 2.5 添加请求体body
|
||||||
|
|
||||||
#### 单个赋值
|
#### 2.5.1 单个赋值
|
||||||
```go
|
```go
|
||||||
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
|
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### map赋值
|
#### 2.5.2 map赋值
|
||||||
```go
|
```go
|
||||||
bodyBulid := map[string]interface{}{
|
bodyBulid := map[string]interface{}{
|
||||||
"beginDate":"2022-03-01",
|
"beginDate":"2022-03-01",
|
||||||
@ -125,32 +125,32 @@ bodyBulid := map[string]interface{}{
|
|||||||
r.Body().Sets(bodyBulid)
|
r.Body().Sets(bodyBulid)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 链式赋值
|
#### 2.5.3 链式赋值
|
||||||
```go
|
```go
|
||||||
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
|
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 字符串赋值
|
#### 2.5.4 字符串赋值
|
||||||
json格式不要使用此方法
|
json格式不要使用此方法
|
||||||
```go
|
```go
|
||||||
bodydata:=`{"devSn":"230000000008","type":"day"}`
|
bodydata:=`{"devSn":"230000000008","type":"day"}`
|
||||||
r.Body().SetString(bodydata)
|
r.Body().SetString(bodydata)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 其它请求参数
|
### 2.6 其它请求参数
|
||||||
|
|
||||||
#### 设置超时时间
|
#### 2.6.1 设置超时时间
|
||||||
```go
|
```go
|
||||||
r.TimeOut(time.Second * 30)
|
r.TimeOut(time.Second * 30)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 跳过证书验证
|
#### 2.6.2 跳过证书验证
|
||||||
```go
|
```go
|
||||||
r.SkipCertVerify()
|
r.SkipCertVerify()
|
||||||
```
|
```
|
||||||
|
|
||||||
### 发送请求
|
### 2.7 发送请求
|
||||||
#### post请求
|
#### 2.7.1 post请求
|
||||||
```go
|
```go
|
||||||
res, err :=r.Post("https://127.0.0.1:80/excample")
|
res, err :=r.Post("https://127.0.0.1:80/excample")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -160,7 +160,7 @@ if err != nil {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### get请求
|
#### 2.7.2 get请求
|
||||||
```go
|
```go
|
||||||
res, err :=r.Get("https://127.0.0.1:80/excample")
|
res, err :=r.Get("https://127.0.0.1:80/excample")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -169,21 +169,21 @@ ftm.Println( "error occured", err)
|
|||||||
fmt.Println(res)
|
fmt.Println(res)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
#### 其它类型的请求
|
#### 2.7.3 其它类型的请求
|
||||||
后续支持...敬请期待
|
后续支持...敬请期待
|
||||||
|
|
||||||
|
|
||||||
### 获取上下文
|
### 2.8 获取上下文
|
||||||
请注意,需要完成请求后才能获得上下文数据!
|
请注意,需要完成请求后才能获得上下文数据!
|
||||||
#### 获取请求的上下文对象
|
#### 2.8.1 获取请求的上下文对象
|
||||||
```go
|
```go
|
||||||
requestContext:=r.Request
|
requestContext:=r.Request
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 获取返回的上下文对象
|
#### 2.8.2 获取返回的上下文对象
|
||||||
```go
|
```go
|
||||||
responseContext:=r.Response
|
responseContext:=r.Response
|
||||||
```
|
```
|
||||||
|
|
||||||
## 使用示例
|
## 3. 使用示例
|
||||||
[simpleRequest_test.go](./test/simpleRequest_test.go)
|
[simpleRequest_test.go](./test/simpleRequest_test.go)
|
Loading…
Reference in New Issue
Block a user