Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idoubi/goz
A fantastic HTTP request libarary used in Golang.
https://github.com/idoubi/goz
curl golang goz http request
Last synced: 2 days ago
JSON representation
A fantastic HTTP request libarary used in Golang.
- Host: GitHub
- URL: https://github.com/idoubi/goz
- Owner: idoubi
- License: mit
- Created: 2017-09-10T14:49:03.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-13T04:47:28.000Z (about 1 year ago)
- Last Synced: 2024-12-03T17:57:11.496Z (10 days ago)
- Topics: curl, golang, goz, http, request
- Language: Go
- Homepage:
- Size: 403 KB
- Stars: 256
- Watchers: 7
- Forks: 59
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - goz
- awesome-hacking-lists - idoubi/goz - A fantastic HTTP request libarary used in Golang. (Go)
README
# goz
A fantastic HTTP request library used in golang. Inspired by [guzzle](https://github.com/guzzle/guzzle)
## Installation
```
go get -u github.com/idoubi/goz
```## Documentation
API documentation can be found here:
https://godoc.org/github.com/idoubi/goz## Basic Usage
```go
package mainimport (
"github.com/idoubi/goz"
)func main() {
cli := goz.NewClient()resp, err := cli.Get("http://127.0.0.1:8091/get")
if err != nil {
log.Fatalln(err)
}fmt.Printf("%T", resp)
// Output: *goz.Response
}
```## Query Params
- query map
```go
cli := goz.NewClient()resp, err := cli.Get("http://127.0.0.1:8091/get-with-query", goz.Options{
Query: map[string]interface{}{
"key1": "value1",
"key2": []string{"value21", "value22"},
"key3": "333",
},
})
if err != nil {
log.Fatalln(err)
}fmt.Printf("%s", resp.GetRequest().URL.RawQuery)
// Output: key1=value1&key2=value21&key2=value22&key3=333
```- query string
```go
cli := goz.NewClient()resp, err := cli.Get("http://127.0.0.1:8091/get-with-query?key0=value0", goz.Options{
Query: "key1=value1&key2=value21&key2=value22&key3=333",
})
if err != nil {
log.Fatalln(err)
}fmt.Printf("%s", resp.GetRequest().URL.RawQuery)
// Output: key1=value1&key2=value21&key2=value22&key3=333
```## Post Data
- post form
```go
cli := goz.NewClient()resp, err := cli.Post("http://127.0.0.1:8091/post-with-form-params", goz.Options{
Headers: map[string]interface{}{
"Content-Type": "application/x-www-form-urlencoded",
},
FormParams: map[string]interface{}{
"key1": "value1",
"key2": []string{"value21", "value22"},
"key3": "333",
},
})
if err != nil {
log.Fatalln(err)
}body, _ := resp.GetBody()
fmt.Println(body)
// Output: form params:{"key1":["value1"],"key2":["value21","value22"],"key3":["333"]}
```- post json
```go
cli := goz.NewClient()resp, err := cli.Post("http://127.0.0.1:8091/post-with-json", goz.Options{
Headers: map[string]interface{}{
"Content-Type": "application/json",
},
JSON: struct {
Key1 string `json:"key1"`
Key2 []string `json:"key2"`
Key3 int `json:"key3"`
}{"value1", []string{"value21", "value22"}, 333},
})
if err != nil {
log.Fatalln(err)
}body, _ := resp.GetBody()
fmt.Println(body)
// Output: json:{"key1":"value1","key2":["value21","value22"],"key3":333}
```## Request Headers
```go
cli := goz.NewClient()resp, err := cli.Post("http://127.0.0.1:8091/post-with-headers", goz.Options{
Headers: map[string]interface{}{
"User-Agent": "testing/1.0",
"Accept": "application/json",
"X-Foo": []string{"Bar", "Baz"},
},
})
if err != nil {
log.Fatalln(err)
}headers := resp.GetRequest().Header["X-Foo"]
fmt.Println(headers)
// Output: [Bar Baz]
```## Response
```go
cli := goz.NewClient()
resp, err := cli.Get("http://127.0.0.1:8091/get")
if err != nil {
log.Fatalln(err)
}body, err := resp.GetBody()
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%T", body)
// Output: goz.ResponseBodypart := body.Read(30)
fmt.Printf("%T", part)
// Output: []uint8contents := body.GetContents()
fmt.Printf("%T", contents)
// Output: stringfmt.Println(resp.GetStatusCode())
// Output: 200fmt.Println(resp.GetReasonPhrase())
// Output: OKheaders := resp.GetHeaders()
fmt.Printf("%T", headers)
// Output: map[string][]stringflag := resp.HasHeader("Content-Type")
fmt.Printf("%T", flag)
// Output: boolheader := resp.GetHeader("content-type")
fmt.Printf("%T", header)
// Output: []string
headerLine := resp.GetHeaderLine("content-type")
fmt.Printf("%T", headerLine)
// Output: string
```## Proxy
```go
cli := goz.NewClient()resp, err := cli.Get("https://www.fbisb.com/ip.php", goz.Options{
Timeout: 5.0,
Proxy: "http://127.0.0.1:1087",
})
if err != nil {
log.Fatalln(err)
}fmt.Println(resp.GetStatusCode())
// Output: 200
```## Timeout
```go
cli := goz.NewClient(goz.Options{
Timeout: 0.9,
})
resp, err := cli.Get("http://127.0.0.1:8091/get-timeout")
if err != nil {
if resp.IsTimeout() {
fmt.Println("timeout")
// Output: timeout
return
}
}fmt.Println("not timeout")
```# License
[MIT](https://opensource.org/licenses/MIT)
Copyright (c) 2017-present, [idoubi](http://idoubi.cc)