Proyectos
Ver en GitHub
asmcos/requests
Acerca de
Una biblioteca cliente HTTP para Go. Un homenaje a Python requests.
Herramientas de desarrolloCrecimiento de estrellas medido
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- -2
- in 1 month
- Recopilando historial
Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.
README.md
Originalrequests
Requests is an HTTP library , it is easy to use. Similar to Python requests.
Installation
go get -u github.com/asmcos/requests
Start
package main
import "github.com/asmcos/requests"
func main (){
resp,err := requests.Get("http://www.jeapedu.com")
if err != nil{
return
}
println(resp.Text())
}
Post
package main
import "github.com/asmcos/requests"
func main (){
data := requests.Datas{
"name":"requests_post_test",
}
resp,_ := requests.Post("https://www.httpbin.org/post",data)
println(resp.Text())
}
Server return data...
{
"args": {},
"data": "",
"files": {},
"form": {
"name": "requests_post_test"
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "www.httpbin.org",
"User-Agent": "Go-Requests 0.5"
},
"json": null,
"origin": "114.242.34.110",
"url": "https://www.httpbin.org/post"
}
PostJson
package main
import "github.com/asmcos/requests"
func main (){
jsonStr := "{\"name\":\"requests_post_test\"}"
resp,_ := requests.PostJson("https://www.httpbin.org/post",jsonStr)
println(resp.Text())
}
Server return data...
{
"args": {},
"data": "",
"files": {},
"form": {
"name": "requests_post_test"
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "www.httpbin.org",
"User-Agent": "Go-Requests 0.5"
},
"json": null,
"origin": "114.242.34.110",
"url": "https://www.httpbin.org/post"
}
Feature Support
- Set headers
- Set params
- Multipart File Uploads
- Sessions with Cookie Persistence
- Proxy
- Authentication
- JSON
- Chunked Requests
- Debug
- SetTimeout
- Skip TLS certificate verification (
SetInsecureSkipVerify)
Changelog (v0.9)
This release was completed with AI coding assistance. The following bugs from GitHub issues and pull requests were fixed:
- #33 / PR #38 — HTTP/2 retry failed with
cannot retry err ... define Request.GetBody. Request bodies are now rewindable viaGetBody. - #37 —
PostJsonsent extra bytes in the body (chunked encoding / trailing newline fromjson.Encoder). Bodies now useContent-Lengthandjson.Marshal. - #10 —
http: ContentLength=N with Body length 0when callingGetafterPoston the same client. Stale body andContent-Lengthare cleared between requests. - PR #36 — Multipart uploads wrote files before form fields, which broke S3-style POSTs that require
keyfirst. Form fields are now written before files. - PR #39 — Errors were printed with
fmt.Println. Errors are returned only, without extra stdout. - #23 / #7 —
Postalways overwroteContent-Type. The defaultapplication/x-www-form-urlencodedis set only when the caller did not set one. - #28 — No way to skip TLS verification (
verify=False). AddedSetInsecureSkipVerify.Proxy()no longer forcesInsecureSkipVerify. - PR #22 —
PostJsonignored URL queryParams. Query parameters are now applied. - Host header —
Header{"Host": "..."}did not change the HTTP Host. It is now applied toRequest.Host. - Resource leaks — Uploaded files and gzip readers were not closed. Both are closed after use.
Set header
example 1
req := requests.Requests()
resp,err := req.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"})
if (err == nil){
println(resp.Text())
}
example 2
req := requests.Requests()
req.Header.Set("accept-encoding", "gzip, deflate, br")
resp,_ := req.Get("http://www.zhanluejia.net.cn",requests.Header{"Referer":"http://www.jeapedu.com"})
println(resp.Text())
example 3
h := requests.Header{
"Referer": "http://www.jeapedu.com",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
}
resp,_ := req.Get("http://wwww.zhanluejia.net.cn",h)
h2 := requests.Header{
...
...
}
h3,h4 ....
// two or more headers ...
resp,_ = req.Get("http://www.zhanluejia.net.cn",h,h2,h3,h4)
Set params
p := requests.Params{
"title": "The blog",
"name": "file",
"id": "12345",
}
resp,_ := req.Get("http://www.cpython.org", p)
Auth
Test with the correct user information.
req := requests.Requests()
resp,_ := req.Get("https://api.github.com/user",requests.Auth{"asmcos","password...."})
println(resp.Text())
github return
{"login":"asmcos","id":xxxxx,"node_id":"Mxxxxxxxxx==".....
JSON
req := requests.Requests()
req.Header.Set("Content-Type","application/json")
resp,_ = req.Get("https://httpbin.org/json")
var json map[string]interface{}
resp.Json(&json)
for k,v := range json{
fmt.Println(k,v)
}
SetTimeout
req := Requests()
req.Debug = 1
// 20 Second
req.SetTimeout(20)
req.Get("http://golang.org")
Get Cookies
resp,_ = req.Get("https://www.httpbin.org")
coo := resp.Cookies()
// coo is [] *http.Cookies
println("********cookies*******")
for _, c:= range coo{
fmt.Println(c.Name,c.Value)
}
Explorar proyectos relacionados
Cargando…