Documentation
¶
Overview ¶
Go HTTP Requests for Humans™.
HTTP Request is so easy:
GET Request
c := &http.Client{}
a := request.NewArgs(c)
resp, err := request.Get("http://httpbin.org/get", a)
j, err := resp.Json()
defer resp.Body.Close() // Don't forget close the response body
POST Request
a.Data = map[string]string{
"key": "value",
"a": "123",
}
resp, err := request.Post("http://httpbin.org/post", a)
Custom Cookies
a.Cookies = map[string]string{
"key": "value",
"a": "123",
}
resp, err := request.Get("http://httpbin.org/cookies", a)
Custom Headers
a.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 := request.Get("http://httpbin.org/get", a)
Upload Files
f, err := os.Open("test.txt")
a.Files = []request.FileField{
request.FileField{"file", "test.txt", f},
}
resp, err := request.Post("http://httpbin.org/post", a)
Json Body
a.Json = map[string]string{
"a": "A",
"b": "B",
}
resp, err := request.Post("http://httpbin.org/post", a)
a.Json = []int{1, 2, 3}
resp, err = request.Post("http://httpbin.org/post", a)
Index ¶
- Constants
- type Args
- type FileField
- type Response
- func Delete(url string, a *Args) (resp *Response, err error)
- func Get(url string, a *Args) (resp *Response, err error)
- func Head(url string, a *Args) (resp *Response, err error)
- func Options(url string, a *Args) (resp *Response, err error)
- func Patch(url string, a *Args) (resp *Response, err error)
- func Post(url string, a *Args) (resp *Response, err error)
- func Put(url string, a *Args) (resp *Response, err error)
Examples ¶
Constants ¶
View Source
const Version = "0.1.0"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Args ¶
type Response ¶
func Get ¶
Example ¶
package main
import (
"fmt"
"net/http"
"github.com/mozillazg/request"
)
func main() {
c := &http.Client{}
a := request.NewArgs(c)
url := "http://httpbin.org/get"
resp, _ := request.Get(url, a)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(resp.Ok())
fmt.Println(d.Get("url").MustString())
}
Output: true http://httpbin.org/get
Example (Cookies) ¶
package main
import (
"net/http"
"github.com/mozillazg/request"
)
func main() {
c := &http.Client{}
a := request.NewArgs(c)
a.Cookies = map[string]string{
"name": "value",
"foo": "bar",
}
url := "http://httpbin.org/cookies"
resp, _ := request.Get(url, a)
defer resp.Body.Close()
}
Output:
Example (CustomHeaders) ¶
package main
import (
"fmt"
"net/http"
"github.com/mozillazg/request"
)
func main() {
c := &http.Client{}
a := request.NewArgs(c)
a.Headers = map[string]string{
"X-Abc": "abc",
"User-Agent": "go-request-test",
}
url := "http://httpbin.org/get"
resp, _ := request.Get(url, a)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(d.Get("headers").Get("User-Agent").MustString())
fmt.Println(d.Get("headers").Get("X-Abc").MustString())
}
Output: go-request-test abc
Example (Params) ¶
package main
import (
"fmt"
"net/http"
"github.com/mozillazg/request"
)
func main() {
c := &http.Client{}
a := request.NewArgs(c)
a.Params = map[string]string{
"a": "1",
"b": "2",
}
url := "http://httpbin.org/get"
resp, _ := request.Get(url, a)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(d.Get("url").MustString())
}
Output: http://httpbin.org/get?a=1&b=2
func Post ¶
Example ¶
package main
import (
"net/http"
"github.com/mozillazg/request"
)
func main() {
c := &http.Client{}
a := request.NewArgs(c)
a.Data = map[string]string{
"a": "1",
"b": "2",
}
url := "http://httpbin.org/post"
resp, _ := request.Post(url, a)
defer resp.Body.Close()
}
Output:
Example (Files) ¶
package main
import (
"net/http"
"os"
"github.com/mozillazg/request"
)
func main() {
c := &http.Client{}
a := request.NewArgs(c)
f, _ := os.Open("test.txt")
a.Files = []request.FileField{
request.FileField{"abc", "abc.txt", f},
}
url := "http://httpbin.org/post"
resp, _ := request.Post(url, a)
defer resp.Body.Close()
}
Output:
Click to show internal directories.
Click to hide internal directories.
