sreqrequestsrequestsnet/http
发送请求
- requests
resp = requests.get('http://httpbin.org/get')
- sreq
resp := sreq.Get("http://httpbin.org/get")
传递 URL 参数
- requests
payload = {
'key1': 'value1',
'key2': 'value2',
}
resp = requests.get('http://httpbin.org/get', params=payload)
- sreq
payload := sreq.Params{
"key1": "value1",
"key2": "value2",
}
resp := sreq.Get("http://httpbin.org/get", sreq.WithQuery(payload))
响应内容
- requests
data = requests.get('http://httpbin.org/get').text
- sreq
// ignore error
data, _ := sreq.Get("http://httpbin.org/get").Text()
二进制响应内容
- requests
data = requests.get('http://httpbin.org/get').content
- sreq
// ignore error
data, _ := sreq.Get("http://httpbin.org/get").Content()
JSON 响应内容
- requests
data = requests.get('http://httpbin.org/get').json()
- sreq
data := make(map[string]interface{})
// ignore error
_ = sreq.Get("http://httpbin.org/get").JSON(&data)
原始响应内容
- requests
data = requests.get('http://httpbin.org/get', stream=True).raw
- sreq
// ignore error
data, _ := sreq.Get("http://httpbin.org/get").Raw()
定制请求头
- requests
headers = {
'Referer': 'https://www.google.com',
}
resp = requests.get('http://httpbin.org/get', headers=headers)
- sreq
headers := sreq.Headers{
"Referer": "https://www.google.com",
}
resp := sreq.Get("http://httpbin.org/get", sreq.WithHeaders(headers))
更加复杂的 POST 请求
- requests
payload = {
'key1': 'value1',
'key2': 'value2',
}
resp = requests.post('http://httpbin.org/post', data=payload)
- sreq
payload := sreq.Form{
"key1": "value1",
"key2": "value2",
}
resp := sreq.Post("http://httpbin.org/post", sreq.WithForm(payload))
上传文件
- requests
files = {
'file': open('report.xls', 'rb'),
}
resp = requests.post('http://httpbin.org/post', files=files)
- sreq
files := sreq.Files{
"file": sreq.MustOpen("report.xls"),
}
resp := sreq.Post("http://httpbin.org/post", sreq.WithMultipart(files, nil))
发送 Cookies
- requests
cookies = {
"name1": "value1",
"name2": "value2",
}
resp = requests.get("http://httpbin.org/get", cookies=cookies)
- sreq
cookies := sreq.Cookies{
"name1": "value1",
"name2": "value2",
}
resp := sreq.Get("http://httpbin.org/get", sreq.WithCookies(cookies))
响应 Cookies
- requests
cookies = requests.get('https://www.xiami.com').cookies
- sreq
// ignore error
cookies, _ := sreq.
Get("https://www.xiami.com").
Cookies()
更多用法可参阅: 或者