sreq 是一个简单、易用以及并发安全的 Golang 网络请求库,灵感来自 Python requests。sreq
提供了 requests 风格的 API,让你可以像使用 requests 那样轻松高效地使用 net/http
。
resp = requests.get('http://httpbin.org/get')
resp := sreq.Get("http://httpbin.org/get")
payload = {
'key1': 'value1',
'key2': 'value2',
}
resp = requests.get('http://httpbin.org/get', params=payload)
payload := sreq.Params{
"key1": "value1",
"key2": "value2",
}
resp := sreq.Get("http://httpbin.org/get", sreq.WithQuery(payload))
data = requests.get('http://httpbin.org/get').text
// ignore error
data, _ := sreq.Get("http://httpbin.org/get").Text()
data = requests.get('http://httpbin.org/get').content
// ignore error
data, _ := sreq.Get("http://httpbin.org/get").Content()
data = requests.get('http://httpbin.org/get').json()
data := make(map[string]interface{})
// ignore error
_ = sreq.Get("http://httpbin.org/get").JSON(&data)
data = requests.get('http://httpbin.org/get', stream=True).raw
// ignore error
data, _ := sreq.Get("http://httpbin.org/get").Raw()
headers = {
'Referer': 'https://www.google.com',
}
resp = requests.get('http://httpbin.org/get', headers=headers)
headers := sreq.Headers{
"Referer": "https://www.google.com",
}
resp := sreq.Get("http://httpbin.org/get", sreq.WithHeaders(headers))
payload = {
'key1': 'value1',
'key2': 'value2',
}
resp = requests.post('http://httpbin.org/post', data=payload)
payload := sreq.Form{
"key1": "value1",
"key2": "value2",
}
resp := sreq.Post("http://httpbin.org/post", sreq.WithForm(payload))
files = {
'file': open('report.xls', 'rb'),
}
resp = requests.post('http://httpbin.org/post', files=files)
files := sreq.Files{
"file": "report.xls",
}
resp := sreq.Post("http://httpbin.org/post", sreq.WithFiles(files))
cookies = {
'name1': 'value1',
}
resp = requests.get("http://httpbin.org/get", cookies=cookies)
cookie := &http.Cookie{
Name: "name1",
Value: "value1",
}
resp := sreq.Get("http://httpbin.org/get", sreq.WithCookies(cookie))
cookies = requests.get('https://www.xiami.com').cookies
// ignore error
cookies, _ := sreq.
Get("https://www.xiami.com").
Cookies()
备注:sreq
不提供后向兼容保证,推荐在个人项目中使用。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.