Skip to content

Commit 46718f2

Browse files
committed
doc: update readme.
1 parent 3ce3824 commit 46718f2

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# HTTP Request tool for Go
22

33
![test](https://github.com/ghosind/go-request/workflows/test/badge.svg)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/ghosind/go-request)](https://goreportcard.com/report/github.com/ghosind/go-request)
45
[![codecov](https://codecov.io/gh/ghosind/go-request/branch/main/graph/badge.svg)](https://codecov.io/gh/ghosind/go-request)
56
![Version Badge](https://img.shields.io/github/v/release/ghosind/go-request)
67
![License Badge](https://img.shields.io/github/license/ghosind/go-request)
@@ -27,6 +28,8 @@ An easy-to-use HTTP request tool for Golang.
2728
- Serialize request body automatically.
2829
- Response body deserialization wrapper.
2930
- Decode the compressed response body automatically.
31+
- Chaining API.
32+
- Request and Response interceptors.
3033

3134
## Installation
3235

@@ -104,6 +107,18 @@ You can also set `Timeout` to `request.RequestTimeoutNone` to disable the timeou
104107

105108
> The timeout will be disabled if you set `Context` in the request config, you need to handle it manually.
106109
110+
### Chaining API
111+
112+
You can also make a request by chaining API:
113+
114+
```go
115+
resp, err := request.Req("http://example.com").
116+
POST().
117+
SetBody(map[string]any{ "title": "Apple" }).
118+
SetTimeout(3000).
119+
Do()
120+
```
121+
107122
### Response body handling
108123

109124
We provided `ToObject` and `ToString` methods to handle response body. For example, the `ToString` method will read all data in the response body, and return it that represented in a string value.
@@ -113,7 +128,6 @@ content, resp, err := ToString(request.Request("https://example.com/products/1")
113128
if err != nil {
114129
// handle error
115130
}
116-
// content: {"id":1,"title":"iPhone9",...
117131
// handle response
118132
```
119133

@@ -129,7 +143,6 @@ product, resp, err := ToObject[Product](request.Request("https://example.com/pro
129143
if err != nil {
130144
// handle error
131145
}
132-
// product: {1 iPhone9}
133146
// handle response
134147
```
135148

README_CN.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Golang HTTP请求工具库
22

33
![test](https://github.com/ghosind/go-request/workflows/test/badge.svg)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/ghosind/go-request)](https://goreportcard.com/report/github.com/ghosind/go-request)
45
[![codecov](https://codecov.io/gh/ghosind/go-request/branch/main/graph/badge.svg)](https://codecov.io/gh/ghosind/go-request)
56
![Version Badge](https://img.shields.io/github/v/release/ghosind/go-request)
67
![License Badge](https://img.shields.io/github/license/ghosind/go-request)
@@ -26,6 +27,8 @@
2627
- 无需手动序列化请求内容,发出请求时自动根据所需格式处理
2728
- 响应内容反序列化封装
2829
- 自动根据响应头部对内容进行解码(解压)
30+
- 链式API
31+
- 请求/响应拦截器
2932

3033
## 安装
3134

@@ -103,6 +106,18 @@ resp, err := request.Request("https://example.com", request.RequestConfig{
103106

104107
> 在通过`Context`属性传入自定义上下文的情况下,将不再执行超时的设定。若需要对请求超时进行控制,则需要进行手动处理。
105108
109+
### 链式API
110+
111+
我们同样提供了链式API用于发起请求,下面是一个链式API的简单示例:
112+
113+
```go
114+
resp, err := request.Req("http://example.com").
115+
POST().
116+
SetBody(map[string]any{ "title": "Apple" }).
117+
SetTimeout(3000).
118+
Do()
119+
```
120+
106121
### 响应内容处理
107122

108123
对于响应的内容,提供了`ToObject``ToString`方法用于简便处理。例如可以使用`ToString`方法读取响应内容,并以字符串的形式返回。
@@ -112,7 +127,6 @@ content, resp, err := ToString(request.Request("https://example.com/products/1")
112127
if err != nil {
113128
// 错误处理
114129
}
115-
// content: {"id":1,"title":"iPhone9",...
116130
// 响应处理
117131
```
118132

@@ -128,7 +142,6 @@ product, resp, err := ToObject[Product](request.Request("https://example.com/pro
128142
if err != nil {
129143
// 错误处理
130144
}
131-
// product: {1 iPhone9}
132145
// 响应处理
133146
```
134147

0 commit comments

Comments
 (0)