Skip to content

Repository files navigation

Request Mentioned in Awesome GoGo Report CardGo

Stargazers over time

Stargazers over time

GoDoccodecovReleaseTODOsLicense

HTTP Client for golang, Inspired by Javascript-axiosPython-request. If you have experience about axios or requests, you will love it. No 3rd dependency.

Features

  • Make http requests from Golang
  • 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

POST

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
varbody=struct {
AstringBint
}{A: "A", B: 1}
varresultinterface{}
c:= request.Client{
URL: "https://google.com",
Method: "POST",
Query: map[string]string{"hello": "world"},
JSON: body,
}
resp:=c.Send().Scan(&result)
if!resp.OK(){
// handle errorlog.Println(resp.Error())
}
// str := resp.String()// bytes := resp.Bytes()

POST with local files

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
c:= request.Client{
URL: "https://google.com",
Method: "POST",
Query: map[string]string{"hello": "world"},
MultipartForm: MultipartForm{
Fields: map[string]string{"a": "1"},
Files: []string{"doc.txt"},
},
}
resp:=c.Send().Scan(&result)
...

POST step by step

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
varresponseinterface{}
resp:=request.
New().
POST("http://httpbin.org/post").
AddHeader(map[string]string{"Google": "google"}).
AddBasicAuth("google", "google").
AddURLEncodedForm(map[string]string{"data": "google"}).
Send().
Scan(&response)
...

POST with context (1/2)

package main
import (
"github.com/monaco-io/request""context"
)
funcmain() {
c:= request.Client{
Context: context.Background(),
URL: "https://google.com",
Method: "POST",
BasicAuth: request.BasicAuth{
Username: "google",
Password: "google",
},
}
resp:=c.Send()
...

POST with context (2/2)

package main
import (
"github.com/monaco-io/request""context"
)
funcmain() {
varresponseinterface{}
resp:=request.
NewWithContext(context.TODO()).
POST("http://httpbin.org/post").
AddHeader(map[string]string{"Google": "google"}).
AddBasicAuth("google", "google").
AddURLEncodedForm(map[string]string{"data": "google"}).
Send().
Scan(&response)
...

Authorization

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
c:= request.Client{
URL: "https://google.com",
Method: "POST",
BasicAuth: request.BasicAuth{
Username: "google",
Password: "google",
},
}
resp:=c.Send()
}

Timeout

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
c:= request.Client{
URL: "https://google.com",
Method: "POST",
Timeout: time.Second*10,
}
}

Cookies

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
c:= request.Client{
URL: "https://google.com",
CookiesMap: map[string]string{
"cookie_name": "cookie_value",
}
}
}

TLS

package main
import (
"github.com/monaco-io/request"
)
funcmain() {
c:= request.Client{
URL: "https://google.com",
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
}

License

MIT

Releases

Sponsor this project

Packages

Used by

Contributors

Languages