reqwest - 简单,易用的 Golang 网络请求库

2019-09-29 12:56:14 +08:00
 winterssy

Features:

Github: https://github.com/winterssy/reqwest

6634 次点击
所在节点    分享创造
29 条回复
AngryPanda
2019-09-29 13:25:43 +08:00
reqeast 啥时候出
littlespider89
2019-09-29 13:36:01 +08:00
yixiang
2019-09-29 13:38:51 +08:00
reqwest - An easy and powerful Rust HTTP Client ( https://github.com/seanmonstar/reqwest)

一看还以为这个 rust 库被移植了。
winterssy
2019-09-29 13:41:05 +08:00
@littlespider89 😅为什么要 PK,纯粹分享下自己的成果而已,无关其它。
winterssy
2019-09-29 13:43:48 +08:00
@yixiang 本来是叫 grequests 的,只是昨晚偶然发现自己搞出来的玩意跟 rust reqwest 的 api 很像,就改成了现在这个名字,之前并不知道 reqwest...
www6688w
2019-09-29 15:21:47 +08:00
支持一下,,( ̄ m  ̄)
www6688w
2019-09-29 15:24:20 +08:00
楼主厉害,,我学了一段时间 Go,感觉还是没有基础吃透

个人英语能力太弱,Go 目前的生态库大部分都是英文文档,学起来吃力
seaguest
2019-09-29 16:48:19 +08:00
支持一下。
目前觉得最好的还是这个:github.com/imroc/req
winterssy
2019-09-29 19:16:05 +08:00
更新:为避免跟 Rust reqwest 引起不必要的误解,项目更名为 sreq。新仓库地址: https://github.com/winterssy/sreq
snowwalf
2019-09-29 19:39:04 +08:00
目前工作中在用这个 https://github.com/go-resty/resty/
zealic
2019-09-29 19:54:28 +08:00
543 次点击 ∙ 3 人收藏 ∙ 1 人感谢

0 STAR
winterssy
2019-09-29 20:06:26 +08:00
@zealic #11 原项目名叫 reqwest 有 7 star 1 fork,后面遇到反馈说碰瓷 Rust 同名项目,删库更名重建了。star 不是我看重的,未曾想跟其它项目对比 /竞争,纯粹分享自己的成果而已。
Aether
2019-09-29 20:29:37 +08:00
@winterssy 讨人厌。
Aether
2019-09-29 20:30:02 +08:00
我说怎么找不到了……还要翻进来重新点一次 star。黑心开发者!
fengkx
2019-09-29 20:46:31 +08:00
支持一下~

学习了代码。然后我想到一个问题。JSON()方法为什么不直接用 json.Decoder 呢。基于这个想法试了一下。


JSON 方法用 Deocder 可以这样
```go
func (r *Response) JSON(v interface{}) error {
dec := json.NewDecoder(r.R.Body)
return dec.Decode(v)
}
```

Bench 代码

```go
func BenchmarkResponse_JSON(b *testing.B) {
type testData struct {
JSON struct {
Msg string `json:"msg"`
Num int `json:"num"`
} `json:"json"`
}
ts := httptest.NewServer( http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
var data testData
dec.Decode(&data.JSON)
enc := json.NewEncoder(w)
enc.Encode(data)
}))
resp := reqwest.Post(ts.URL).
JSON(
reqwest.Data{
"msg": "hello world",
"num": 2019,
}).
Send().
EnsureStatusOk()
b.ResetTimer()
for i:=0;i<b.N;i++ {
var retData testData
resp.JSON(&retData)
}
}
```

结果如下
## Unmarshal
```plain
$ go test -v -run="none" -bench="BenchmarkResponse_JSON" -benchtime="3s" -benchmem
goos: linux
goarch: amd64
pkg: github.com/winterssy/reqwest
BenchmarkResponse_JSON-4 5000000 959 ns/op 544 B/op 2 allocs/op
PASS
ok github.com/winterssy/reqwest 5.895s
```
## Decoder
```plain
$ go test -v -run="none" -bench="BenchmarkResponse_JSON" -benchtime="3s" -benchmem
goos: linux
goarch: amd64
pkg: github.com/winterssy/reqwest
BenchmarkResponse_JSON-4 10000000 713 ns/op 896 B/op 3 allocs/op
PASS
ok github.com/winterssy/reqwest 7.758s
```

跟我设想的不太一样。还以为 Decoder 会更快。为什么会这样呢?
whoami9894
2019-09-29 21:10:50 +08:00
winterssy
2019-09-29 21:22:54 +08:00
@fengkx #15 我在 windows 上测了一下,结果反而跟你预期的一样,decoder 性能更好。这可能跟测试环境有关,网上查了一下,选择 decoder 更为合适,后续会使用 decoder 替换 unmarshal,感谢反馈。

stackoverflow 上类似的提问: https://stackoverflow.com/questions/21197239/decoding-json-using-json-unmarshal-vs-json-newdecoder-decode

附上测试结果:
## unmarshal
goos: windows
goarch: amd64
pkg: github.com/winterssy/sreq
BenchmarkResponse_JSON-4 7664007 480 ns/op 544 B/op 2 allocs/op
PASS
ok github.com/winterssy/sreq 4.558s

## decoder
goos: windows
goarch: amd64
pkg: github.com/winterssy/sreq
BenchmarkResponse_JSON-4 8721448 409 ns/op 896 B/op 3 allocs/op
PASS
ok github.com/winterssy/sreq 4.386s
winterssy
2019-09-29 21:37:42 +08:00
@fengkx #15 不对,你的测试结果也表示 decoder 更快吧。decoder 比 unmarshal 更快,但貌似需要更多的内存分配次数跟空间
justin2018
2019-09-29 22:39:39 +08:00
nice 已 start
deletelazy
2019-09-29 22:41:41 +08:00
你这个库有点问题啊,不是 thread-safe 的。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/605342

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX