/restly

Fast REST client for Go (golang)

Primary LanguageGoMIT LicenseMIT

restly - Fast REST client for Go (golang)

Go Report GolangCI Travis codecov License Maintenance GoDoc

This fast REST client combines fasthttp for performance, gjson for JSON searching, and etree for XML searching.

Features:

  • Fully configurable fasthttp.Request struct
  • Automatic encoding REST routes with client libraries (gjson & etree) with support for problem errors RFC7807
  • Automatic encoding for query string parameters
  • About 100 LOC
  • About 10x faster than using net/http
go get github.com/apibillme/restly
req := restly.New()

req.Header.Add("Authorization", "Bearer my_token")

jsonBody := `{"key":"value"}`

xmlBody := `<?xml version="1.0" encoding="UTF-8"?><People><Person name="Jon"/></People></xml>`

res, statusCode, err := restly.GetJSON(req, "https://mockbin.com/request", "?foo=bar")
	
res, statusCode, err := restly.DeleteJSON(req, "https://mockbin.com/request", "?foo=bar")
	
res, statusCode, err := restly.PutJSON(req, "https://mockbin.com/request", jsonBody, "?foo=bar")
	
res, statusCode, err := restly.PostJSON(req, "https://mockbin.com/request", jsonBody, "?foo=bar")
	
res, statusCode, err := restly.PatchJSON(req, "https://mockbin.com/request", jsonBody, "?foo=bar")

res, statusCode, err := restly.GetXML(req, "https://mockbin.com/request", "?foo=bar")
	
res, statusCode, err := restly.DeleteXML(req, "https://mockbin.com/request", "?foo=bar")
	
res, statusCode, err := restly.PutXML(req, "https://mockbin.com/request", xmlBody, "?foo=bar")
	
res, statusCode, err := restly.PostXML(req, "https://mockbin.com/request", xmlBody, "?foo=bar")
	
res, statusCode, err := restly.PatchXML(req, "https://mockbin.com/request", xmlBody, "?foo=bar")

Motivation

I saw the largest problem with Go (Golang) being interacting with JSON & XML with REST clients. At the time the popular REST clients required you to strongly type out each interface you need for the request. This is painful and slow!

I wanted a one-liner request with the ability to dynamically set, find, and extract values from JSON & XML without all the boilerplate of a net/http request. This library delivers you exactly these requirements!

The request body is simply a string and the find/extract interface relies on battle-tested libraries for either JSON (gjson) or XML (etree).

Because this library uses fasthttp rather than net/http it is about 10x faster than competing libraries. It is also only about 100 LOC compared to the massive codebases of competing projects.