HTTP REST client, simplified for Go
Here is a quick example:
GET /your/api/{param}/call
package main
import "github.com/sendgrid/rest"
import "fmt"
func main() {
host := "https://api.example.com"
endpoint := "/your/api/" + param + "/rest"
baseURL := host + endpoint
requestHeaders := make(map[string]string)
requestHeaders["Authorization"] = "Bearer " + Key
method := "GET"
request := rest.Request{
Method: method,
BaseURL: baseURL,
RequestHeaders: requestHeaders
}
response, e := rest.API(request)
if e != nil {
fmt.Println(e)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.ResponseBody)
fmt.Println(response.ResponseHeaders)
}
}
POST /your/api/{param}/call
with headers, query parameters and a request body.
package main
import "github.com/sendgrid/rest"
import "fmt"
func main() {
host := "https://api.example.com"
endpoint := "/your/api/" + param + "/rest"
baseURL := host + endpoint
requestHeaders := make(map[string]string)
requestHeaders["Authorization"] = "Bearer " + Key
requestHeaders["X-Test"] = "Test"
var requestBody = []byte(`{"some": 0, "awesome": 1, "data": 3}`)
queryParams := make(map[string]string)
queryParams["hello"] = 0
queryParams["world"] = 1
method := "POST"
request = rest.Request{
Method: method,
BaseURL: baseURL,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
RequestBody: requestBody,
}
response, e := rest.API(request)
if e != nil {
fmt.Println(e)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.ResponseBody)
fmt.Println(response.ResponseHeaders)
}
}
go get github.com/sendgrid/rest
Following is an example using SendGrid. You can get your free account here.
First, update your environment with your SENDGRID_API_KEY and HOST. For this example HOST=https://api.sendgrid.com.
echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
Following is an abridged example, here is the full working code.
package main
import (
"encoding/json"
"fmt"
"github.com/sendgrid/rest"
"os"
)
func main() {
// Build the URL
host := "https://api.sendgrid.com"
endpoint := "/v3/api_keys"
baseURL := host + endpoint
// Build the request headers
key := os.Getenv("SENDGRID_API_KEY")
requestHeaders := make(map[string]string)
requestHeaders["Content-Type"] = "application/json"
requestHeaders["Authorization"] = "Bearer " + key
// GET Collection
method := "GET"
// Build the query parameters
queryParams := make(map[string]string)
queryParams["limit"] = "100"
queryParams["offset"] = "0"
// Make the API call
request := rest.Request{
Method: method,
BaseURL: baseURL,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
}
response, e := rest.API(request)
// POST
method = "POST"
var requestBody = []byte(` {
"name": "My API Key",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}`)
request = rest.Request{
Method: method,
BaseURL: baseURL,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
RequestBody: requestBody,
}
response, e = rest.API(request)
// Get a particular return value.
// Note that you can unmarshall into a struct if
// you know the JSON structure in advance.
b := []byte(response.ResponseBody)
var f interface{}
err := json.Unmarshal(b, &f)
if err != nil {
fmt.Println(err)
}
m := f.(map[string]interface{})
apiKey := m["api_key_id"].(string)
// GET Single
method = "GET"
// Make the API call
request = rest.Request{
Method: method,
BaseURL: baseURL + "/" + apiKey,
RequestHeaders: requestHeaders,
}
response, e = rest.API(request)
// PATCH
method = "PATCH"
requestBody = []byte(`{
"name": "A New Hope"
}`)
request = rest.Request{
Method: method,
BaseURL: baseURL + "/" + apiKey,
RequestHeaders: requestHeaders,
RequestBody: requestBody,
}
response, e = rest.API(request)
// PUT
method = "PUT"
requestBody = []byte(`{
"name": "A New Hope",
"scopes": [
"user.profile.read",
"user.profile.update"
]
}`)
request = rest.Request{
Method: method,
BaseURL: baseURL + "/" + apiKey,
RequestHeaders: requestHeaders,
RequestBody: requestBody,
}
response, e = rest.API(request)
// DELETE
method = "DELETE"
request = rest.Request{
Method: method,
BaseURL: baseURL + "/" + apiKey,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
RequestBody: requestBody,
}
response, e = rest.API(request)
}
[2016.04.05] - We hit version 1!
We encourage contribution to our libraries, please see our CONTRIBUTING guide for details.
![SendGrid Logo] (https://assets3.sendgrid.com/mkt/assets/logos_brands/small/sglogo_2015_blue-9c87423c2ff2ff393ebce1ab3bd018a4.png)
rest is guided and supported by the SendGrid Developer Experience Team.
rest is maintained and funded by SendGrid, Inc. The names and logos for python-http-client are trademarks of SendGrid, Inc.