A lightweight package that makes it easier to send Rest and GraphQL requests in Golang. For every request you make, Goaxios returns the http response object, the raw response body in bytes, the parsed response body in a struct or interface, and an error object.
- Create and run REST HTTP requests
- Basic configuration for REST HTTP requests
- Validate Goaxios Struct before running request
- Interceptors for before Request and after Response
- Multipart form data requests
- Create and run GraphQL HTTP requests
- Basic configuration for GraphQL HTTP requests
- Download file to a location
- Download file to a writer
- Upload file from a source
- Upload and download progress
- JavaScript
Promise.all()
style to run multiple requests - Auto build path from path parameters like
/users/:id
go get github.com/opensaucerer/goaxios
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
func main() {
a := goaxios.GoAxios{
Url: "https://anapioficeandfire.com/api/houses/1",
Method: "GET",
}
r, b, d, err := a.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Printf("Response Object: ", r)
fmt.Printf("Raw Body in Bytes: ", b)
fmt.Printf("Parsed Body: ", d)
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
func main() {
a := goaxios.GoAxios{
Url: "https://anapioficeandfire.com/api/:id",
Params: map[string]string{
"id": "houses",
},
Method: "POST",
Query: map[string]string{
"name": "House Stark",
"region": "The North",
},
}
_, _, _, err := a.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
func main() {
a := goaxios.GoAxios{
Url: "https://anapioficeandfire.com/api/houses",
Method: "POST",
Body: map[string]string{
"name": "House Stark",
"region": "The North",
},
}
_, _, _, err := a.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
type House struct {
Name string `json:"name"`
Region string `json:"region"`
Words string `json:"words"`
Seats []string `json:"seats"`
}
func main() {
a := goaxios.GoAxios{
Url: "https://anapioficeandfire.com/api/houses/1",
Method: "GET",
ResponseStruct: &House{},
}
_, _, _, err := a.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
type ResponseStruct struct {
Login string `json:"login"`
Id int `json:"id"`
NodeID string `json:"node_id"`
URL string `json:"url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
HooksURL string `json:"hooks_url"`
IssuesURL string `json:"issues_url"`
MembersURL string `json:"members_url"`
PublicMembersURL string `json:"public_members_url"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
}
func main() {
token := ""
a := goaxios.GoAxios{
Url: "https://api.github.com/user/orgs",
Method: "GET",
ResponseStruct: &[]ResponseStruct{},
BearerToken: token,
}
_, _, response, err := a.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Println(response)
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
func main() {
r := goaxios.GoAxios{
Url: "https://api.twitter.com/2/oauth2/token",
Method: "POST",
Headers: map[string]string{
// needs to be empty to prevent goaxios from setting content-type to application/json
},
Query: map[string]interface{}{
"grant_type": "refresh_token",
"refresh_token": refreshToken,
"client_id": config.Env.TwitterClientID,
},
}
_, _, response, err := r.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Println(response)
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
func main() {
r := goaxios.GoAxios{
Url: "https://api.pinata.cloud/pinning/pinFileToIPFS",
Method: "POST",
Form: &Form{
Files: []FormFile{
{
Name: "somefile.json",
Path: os.Getenv("LOCATION"),
Key: "file",
},
},
},
BearerToken: os.Getenv("TOKEN"),
}
_, _, response, err := r.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Println(response)
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
func main() {
f, _ := os.Open(os.Getenv("LOCATION"))
r := GoAxios{
Url: "https://api.pinata.cloud/pinning/pinFileToIPFS",
Method: "POST",
Form: &Form{
Files: []FormFile{
{
Name: "somefile.json",
Handle: f,
Key: "file",
},
},
},
BearerToken: os.Getenv("TOKEN"),
}
_, _, response, err := r.RunRest()
if err != nil {
fmt.Printf("err: %v", err)
}
fmt.Println(response)
}
package main
import (
"log"
"github.com/opensaucerer/goaxios"
)
func main() {
a := goaxios.GoAxios{
Url: "https://media.publit.io/file/wm_22a67238/castorr.webm",
Method: "GET",
IsDownload: true,
DownloadDestination: goaxios.Destination{
Location: "test.webm",
},
}
_, _, _, err := a.RunRest()
if err != nil {
log.Fatalf("err: %v", err)
}
}
package main
import (
"log"
"os"
"github.com/opensaucerer/goaxios"
)
func main() {
// create a file
w, err := os.Create("castorr.webm")
if err != nil {
log.Fatalf("err: %v", err)
}
defer w.Close()
a := goaxios.GoAxios{
Url: "https://media.publit.io/file/wm_22a67238/castorr.webm",
Method: "GET",
IsDownload: true,
DownloadDestination: goaxios.Destination{
Writer: w,
},
}
_, _, _, err = a.RunRest()
if err != nil {
log.Fatalf("err: %v", err)
}
}
package main
import (
"fmt"
"github.com/opensaucerer/goaxios"
)
a := goaxios.GoAxios{
Url: "https://api.twitter.com/2/oauth2/token",
Method: "POST",
Interceptor: goaxios.Interceptor{
Request: func(req *goaxios.GoAxios) *goaxios.GoAxios {
// modify the request as needed
req.BearerToken = "token"
req.Headers = map[string]string{
"Content-Type": "application/json",
}
req.Body = map[string]string{
"key": "value",
}
return req
},
Response: func(resp *http.Response) *http.Response {
// do something with the response - logging/error handling or something
if resp.StatusCode != 200 {
panic("not OK")
}
return resp
},
},
}
_, _, response, err := a.RunRest()
if err != nil {
t.Errorf("err: %v", err)
}
fmt.Println(response)
Contributions are welcome. Please open a pull request or open an issue to discuss the change you wish to make.