Go HTTP Requests. ✨🎉✨ Send requests quickly and humanely.
Get module.
go get -u github.com/Kaiser925/requests4go
package main
import (
"log"
"github.com/Kaiser925/requests4go"
)
func main() {
r, err := requests4go.Get("http://httpbin.org/get")
if err != nil {
log.Fatal(err.Error())
}
txt, _ := r.Text()
log.Println(txt)
}
You can also send a POST request.
package main
import (
"log"
"github.com/Kaiser925/requests4go"
)
func main() {
// JSON will set body be json data.
data := requests4go.JSON(requests4go.M{"key": "value"})
r, err := requests4go.Post("http://httpbin.org/post", data)
// handle r and err
}
params := requests4go.Params(requests4go.M{"key1": "value1", "key2": "value2"})
r, err := requests4go.Get("http://httpbin.org/get", params)
headers := requests4go.Headers(requests4go.M{"key1": "value1", "key2": "value2"})
r, err := requests4go.Get("http://httpbin.org/get", headers)
We can read the content of the server's response.
resp, _ := requests4go.Get("https://httpbin.org/get", nil)
txt, _ := resp.Text()
log.Println(txt)
There are two methods to handle JSON response content.
- We can unmarshal the struct by using JSON.
foo := &Foo{}
resp, _ := requests4go.Get("https://example.com")
j, _ := resp.JSON(&foo)
fmt.Printf("%v\n", foo)
- Struct implements Unmarshaler.
package foo
type Foo struct {}
type (f *Foo) Unmarshal(p []byte) error {
// ... unmarshal logic
return nil
}
func F() {
f := &Foo{}
resp, _ := requests4go.Get("https://example.com")
err := resp.Unmarshal(f)
if err != nil {
// error handle
}
fmt.Printf("%v\n", foo)
}
Apache License, Version 2.0. See LICENSE for the full license text