mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-15 08:29:25 +01:00
a0741d99b8
Signed-off-by: Gary Kim <gary@garykim.dev>
4.7 KiB
4.7 KiB
Request
HTTP client for golang, Inspired by Javascript-axios Python-request. If you have experience about axios or requests, you will love it. No 3rd dependency.
Features
- Make http requests from Golang
- Intercept request and response
- Transform request and response data
Installing
go mod:
go get github.com/monaco-io/request
Methods
- OPTIONS
- GET
- HEAD
- POST
- PUT
- DELETE
- TRACE
- CONNECT
Example
GET
package main
import (
"log"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL: "GET",
Method: map[string]string{"hello": "world"},
Params}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}
POST
package main
import (
"log"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL: "POST",
Method: map[string]string{"hello": "world"},
Params: []byte(`{"hello": "world"}`),
Body}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}
Content-Type
package main
import (
"log"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL: "POST",
Method: request.ApplicationXWwwFormURLEncoded, // default is "application/json"
ContentType}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}
Authorization
package main
import (
"log"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL: "POST",
Method: request.BasicAuth{
BasicAuth:"user_xxx",
Username:"pwd_xxx",
Password}, // xxx:xxx
}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}
Timeout
package main
import (
"log"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL: "POST",
Method: 10, // seconds
Timeout}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}
Cookies
package main
import (
"log"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL:[]*http.Cookie{
Cookies{
: "cookie_name",
Name: "cookie_value",
Value},
},
}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}
TLS
package main
import (
"log"
"crypto/tls"
"github.com/monaco-io/request"
)
func main() {
:= request.Client{
client : "https://google.com",
URL: &tls.Config{InsecureSkipVerify: true},
TLSConfig}
, err := client.Do()
resp
.Println(resp.Code, string(resp.Data), err)
log}