Package gjrc
offers generic utilities to work with JSON-based RESTful API.
$ go get -u github.com/btnguyen2k/consu/gjrc
package main
import (
"fmt"
"time"
"github.com/btnguyen2k/consu/gjrc"
"github.com/btnguyen2k/consu/reddo"
)
func main() {
// // pre-build a http.Client
// httpClient := &http.Client{}
// client := NewGjrc(httpClient, 0)
// or, a new http.Client is created with 10 seconds timeout
client := gjrc.NewGjrc(nil, 10*time.Second)
url := "https://httpbin.org/post"
resp := client.PostJson(url, map[string]interface{}{"key1": "value1", "key2": 2, "key3": true})
val1, err := resp.GetValueAsType("json.key1", reddo.TypeString)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", val1) // output: "value1"
val2, err := resp.GetValueAsType("json.key2", reddo.TypeInt)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", val2) // output: 2
val3, err := resp.GetValueAsType("json.key3", reddo.TypeBool)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", val3) // output: true
}
This project is licensed under the MIT License - see the LICENSE.md file for details.
Feel free to create pull requests or issues to report bugs or suggest new features. Please search the existing issues before filing new issues to avoid duplicates. For new issues, file your bug or feature request as a new issue.
If you find this project useful, please star it.