Skip to content

Latest commit

 

History

History
62 lines (52 loc) · 1.48 KB

TROUBLESHOOTING.md

File metadata and controls

62 lines (52 loc) · 1.48 KB

Table of Contents

Viewing the Request Body

When debugging or testing, it may be useful to examine the raw request body to compare against the documented format.

Example Code

package main

import "github.com/sendgrid/rest"
import "fmt"

func main() {
	const host = "https://api.example.com"
	param := "myparam"
	endpoint := "/your/api/" + param + "/call"
	baseURL := host + endpoint
	Headers := make(map[string]string)
	key := os.Getenv("API_KEY")
	Headers["Authorization"] = "Bearer " + key
	Headers["X-Test"] = "Test"
	var Body = []byte(`{"some": 0, "awesome": 1, "data": 3}`)
	queryParams := make(map[string]string)
	queryParams["hello"] = "0"
	queryParams["world"] = "1"
	method := rest.Post
	request = rest.Request{
		Method:      method,
		BaseURL:     baseURL,
		Headers:     Headers,
		QueryParams: queryParams,
		Body:        Body,
	}
	response, err := rest.API(request)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(response.StatusCode)
		fmt.Println(response.Body)
		fmt.Println(response.Headers)
	}
}

You can do this right before you call response, err := rest.API(request) like so:

fmt.Printf("Request Body: %v \n", string(request.Body))

req, e := BuildRequestObject(request)
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
	t.Errorf("Error : %v", err)
}
fmt.Printf("Request : %v \n", string(requestDump))