Categorygithub.com/kevinburke/rest
modulepackage
0.0.0-20240617045629-3ed0ad3487f0
Repository: https://github.com/kevinburke/rest.git
Documentation: pkg.go.dev

# README

rest

This library contains a HTTP client, and a number of useful middlewares for writing a HTTP client and server in Go. For more information and package documentation, please see the godoc documentation.

Client

The Client struct makes it easy to interact with a JSON API.

client := restclient.New("username", "password", "http://ipinfo.io")
req, _ := client.NewRequest("GET", "/json", nil)
type resp struct {
    City string `json:"city"`
    Ip   string `json:"ip"`
}
var r resp
client.Do(req, &r)
fmt.Println(r.Ip)

Transport

Use the restclient.Transport as the http.Transport to easily inspect the raw HTTP request and response. Set DEBUG_HTTP_TRAFFIC=true in your environment to dump HTTP requests and responses to stderr.

Defining Custom Error Responses

rest exposes a number of HTTP error handlers - for example, rest.ServerError(w, r, err) will write a 500 server error to w. By default, these error handlers will write a generic JSON response over the wire, using fields specified by the HTTP problem spec.

You can define a custom error handler if you like (say if you want to return a HTML server error, or 404 error or similar) by calling RegisterHandler:

rest.RegisterHandler(500, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    err := rest.CtxErr(r)
    fmt.Println("Server error:", err)
    w.Header().Set("Content-Type", "text/html")
    w.WriteHeader(500)
    w.Write([]byte("<html><body>Server Error</body></html>"))
}))

Debugging

Set the DEBUG_HTTP_TRAFFIC environment variable to print out all request/response traffic being made by the client.

rest also includes a Transport that is a drop in for a http.Transport, but includes support for debugging HTTP requests. Add it like so:

client := http.Client{
    Transport: &restclient.Transport{
        Debug: true,
        Output: os.Stderr,
        Transport: http.DefaultTransport,
    },
}

Donating

Donations free up time to make improvements to the library, and respond to bug reports. You can send donations via Paypal's "Send Money" feature to [email protected]. Donations are not tax deductible in the USA.

# Packages

No description provided by the author
No description provided by the author

# Functions

BadRequest logs a 400 error and then returns a 400 response to the client.
CtxDomain returns a domain that's been set on the request.
CtxErr returns an error that's been stored in the Request context.
Forbidden returns a 403 Forbidden status code to the client, with the given Error object in the response body.
Gone responds to the request with a 410 Gone error message.
NoContent returns a 204 No Content message.
NotAllowed returns a generic HTTP 405 Not Allowed status and response body to the client.
NotFound returns a 404 Not Found error to the client.
RegisterHandler registers the given HandlerFunc to serve HTTP requests for the given status code.
ServerError logs the error to the Logger, and then responds to the request with a generic 500 server error message.
Unauthorized sets the Domain in the request context.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Logger logs information about incoming requests.
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author