It's a package that makes it easy to send HTTP requests using Go. I had the idea to code this package because I was tired to write the same code every time I wanted to interact with an external server and I thought it can be useful for someone else with the same problem
go get -u github.com/jaavier/go-requests
Methods allowed:
- GET
- POST (JSON or Form)
- PUT
- DELETE
- Headers
type Header struct {
Key string
Value string
}
- Request
type Request struct {
Url string
Method string
Headers []Header
BodyJSON interface{}
Form url.Values
}
- Response
type Response struct {
Body string
StatusCode int
Headers http.Header
Cookies []*http.Cookie
ContentLength int64
}
package main
import (
"fmt"
gorequests "github.com/jaavier/go-requests"
)
func main() {
res, err := gorequests.SendRequest(gorequests.Request{
Method: "GET",
Url: "https://ifconfig.co",
Headers: []gorequests.Header{
{
Key: "User-Agent",
Value: "curl/7.84.0",
},
},
})
if err != nil {
panic(err)
}
fmt.Printf("Your ip is: %s", res.Body)
}
package main
import (
"fmt"
gorequests "github.com/jaavier/go-requests"
)
type Visitor struct {
Message string `json:"message"`
}
func main() {
response, err := gorequests.SendRequest(gorequests.Request{
Method: "POST",
Url: "https://webhook.site/a41a84e0-1659-4abb-8e06-6a3a1e23e95c",
BodyJSON: Visitor{
Message: "Hello from github.com/jaavier/go-requests!",
},
Headers: []gorequests.Header{
{
Key: "Content-Type",
Value: "application/json",
},
},
})
if err != nil {
panic(err)
}
fmt.Println(response.Body)
}
package main
import (
"fmt"
"net/url"
gorequests "github.com/jaavier/go-requests"
)
type Visitor struct {
Message string `json:"message"`
}
func main() {
var form url.Values = url.Values{
"message": {"Hello from github.com/jaavier/requests"},
}
response, err := gorequests.SendRequest(gorequests.Request{
Method: "POST",
Url: "https://webhook.site/a41a84e0-1659-4abb-8e06-6a3a1e23e95c",
Form: form,
})
if err != nil {
panic(err)
}
fmt.Println(response.StatusCode)
}