Skip to content

AB-Lindex/go-resthelp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simplified wrapper for calling REST-APIs

Installation

go get github.com/AB-Lindex/go-resthelp

Examples

Create a helper

helper,err := resthelp.New(
  resthelp.WithBaseURL("https://some-custom-api.com"),
  resthelp.WithHeader("Authentication", "Bearer " + token),
)

Create a Request

// Simple GET request with a query parameter
req,err := helper.Get("/users",
  resthelp.WithQuery("page", "2"),
)

// Simple POST with a JSON body
var body myStruct
req,err := helper.Post("/users",
  resthelp.WithJSON(&body),
)

Execute a Request and handle the Response

// Execute the request
resp,err := req.Do()
defer resp.Close()     // always close the response (this is nil-safe)
if !resp.IsOK() {      // this checks for 'err' or anything except 2xx
  panic(resp.Error())  // will either return 'err' or the status-text
}
var data myReponseStruct
err = resp.Parse(data) // will parse the response body into 'data' according to Content-Type