Skip to content
A developer-friendly HTTP request library for Gopher.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
Godeps use godep for old version Sep 3, 2016
_example support before and after request hook. closed #17 Dec 11, 2016
.editorconfig
.gitignore
.travis.yml
CHANGELOG.md v0.8.0 Dec 20, 2016
LICENSE
Makefile gofmt and golint Aug 26, 2016
README.md
cookies.go
cookies_test.go
doc.go update docs Sep 4, 2016
example_test.go
form.go
form_test.go
glide.yaml use glide instead of godep Aug 26, 2016
headers.go
headers_test.go
hook.go
hook_test.go
proxy.go
proxy_go12.go update proxy configure Aug 28, 2015
proxy_test.go
redirect.go
redirect_test.go add ErrMaxRedirect Dec 18, 2016
request.go
request_test.go
response.go
response_test.go improve tests about redirect and response Dec 17, 2016

README.md

request

Build Status Coverage Status Go Report Card GoDoc

A developer-friendly HTTP request library for Gopher. Inspired by Python-Requests.

Installation

go get -u github.com/mozillazg/request

Documentation

API documentation can be found here: https://godoc.org/github.com/mozillazg/request

Usage

import (
    "github.com/mozillazg/request"
)

GET:

c := new(http.Client)
req := request.NewRequest(c)
resp, err := req.Get("http://httpbin.org/get")
j, err := resp.Json()
defer resp.Body.Close()  // Don't forget close the response body

POST:

req := request.NewRequest(c)
req.Data = map[string]string{
    "key": "value",
    "a":   "123",
}
resp, err := req.Post("http://httpbin.org/post")

Cookies:

req := request.NewRequest(c)
req.Cookies = map[string]string{
    "key": "value",
    "a":   "123",
}
resp, err := req.Get("http://httpbin.org/cookies")

Headers:

req := request.NewRequest(c)
req.Headers = map[string]string{
    "Accept-Encoding": "gzip,deflate,sdch",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
}
resp, err := req.Get("http://httpbin.org/get")

Files:

req := request.NewRequest(c)
f, err := os.Open("test.txt")
req.Files = []request.FileField{
    request.FileField{"file", "test.txt", f},
}
resp, err := req.Post("http://httpbin.org/post")

Json:

req := request.NewRequest(c)
req.Json = map[string]string{
    "a": "A",
    "b": "B",
}
resp, err := req.Post("http://httpbin.org/post")
req.Json = []int{1, 2, 3}
resp, err = req.Post("http://httpbin.org/post")

Proxy:

req := request.NewRequest(c)
req.Proxy = "http://127.0.0.1:8080"
// req.Proxy = "https://127.0.0.1:8080"
// req.Proxy = "socks5://127.0.0.1:57341"
resp, err := req.Get("http://httpbin.org/get")

or https://github.com/mozillazg/request/tree/develop/_example/proxy

HTTP Basic Authentication:

req := request.NewRequest(c)
req.BasicAuth = request.BasicAuth{"user", "passwd"}
resp, err := req.Get("http://httpbin.org/basic-auth/user/passwd")

License

Under the MIT License.

You can’t perform that action at this time.