- 原包名 httpurl 看不出是客户端, 更名 httpc; GET/POST/PUT/DELETE/代理/ Digest 认证, 函数签名不变, 附测试、例程与 README
42 lines
1.3 KiB
Markdown
42 lines
1.3 KiB
Markdown
# httpc
|
||
|
||
轻量 HTTP 客户端:GET/POST/PUT/DELETE 快捷方法(自定义 Header、可选代理)、
|
||
Digest 认证请求。
|
||
|
||
> 迁移自 go-hua/httpurl 并更名:原包名 `httpurl` 看不出是客户端,更名 `httpc`
|
||
> (http client)。函数签名不变。
|
||
|
||
## 用法
|
||
|
||
```go
|
||
import "git.zeroonesoft.cn/golib/zogo/httpc"
|
||
|
||
body, err := httpc.Get("https://api.example.com/v1/info", nil)
|
||
body, err := httpc.Post("https://api.example.com/v1/add", []byte(`{"id":1}`),
|
||
map[string]string{"Content-Type": "application/json"})
|
||
body, err := httpc.GetProxy(url, nil, "http://127.0.0.1:8080") // 走代理
|
||
body, err := httpc.Put(url, payload, nil)
|
||
body, err := httpc.Delete(url, nil, nil)
|
||
|
||
// Digest 认证
|
||
resp, err := httpc.DigestAuthRequest(&httpc.RequestArg{
|
||
Url: url,
|
||
Method: "GET",
|
||
Username: "admin",
|
||
Password: "pass",
|
||
})
|
||
```
|
||
|
||
完整可运行例程:[examples/httpc/main.go](../examples/httpc/main.go)
|
||
|
||
## 返回与错误
|
||
|
||
- 2xx 返回响应体字节集;非 2xx 返回包含状态码的错误。
|
||
- `Get/Post/Put/Delete` 的 `header` 参数传 `nil` 表示不附加自定义头。
|
||
|
||
## 注意
|
||
|
||
- `DigestAuthRequest` 只处理 RFC 2617 Digest 流程(qop=auth);Basic/Bearer
|
||
请直接在 header 里带 Authorization。
|
||
- 需要 JSON 自动绑定/泛型封装时配合服务端包 `zogo/httpx` 之外的客户端封装自行扩展。
|