Parcel - HTTP rendering and binding library for Go

parcel

HTTP rendering/binding library for Go

Getting Started

Add to your project

go get github.com/jaredhughes1012/parcel

Usage

Parcel uses renderers/binders globally based on the type of data received. When binding from sources, this detection is done using the "Content-Type" header of the source request or response. When rendering to a target, this uses the type of data passed in for rendering (e.g. text/plain for strings, application/json for structs)

All renderers/binders for a given data type can be modified globally using the corresponding Set method. For example, to change from a json renderer to another type

func Example() {
  parcel.SetObjectRenderer(anothertype.Renderer{})
}

Custom binders/renderers can be created simply by implementing the Renderer or Binder interfaces. Simply pass your custom renderer or binder into one of these global setters to use it globally.

Parcel also supports adding HTTP response data to errors and automatically rendering those errors. By default parcel formats error bodies as JSON objects but plain text can be used as well. These wrapped errors include a status which is automatically set on the response.

Examples

Bind JSON data from Request

type Example struct {
  Field string `json:"field"`
}

func sampleHandler(w http.ResponseWriter, r *http.Request) {
  var data Example
  if err := parcel.BindRequest(r, &data); err != nil {
    parcel.RenderErrorResponse(w, err) // Parcel will return correct http status based on error type
  }
}

Bind text data from Request

func sampleHandler(w http.ResponseWriter, r *http.Request) {
  var data string

  // Parcel will automatically validate text content by using a string receiver
  if err := parcel.BindRequest(r, &data); err != nil {
    parcel.RenderErrorResponse(w, err)
  }
}

Render JSON to response

type Example struct {
  Field string `json:"field"`
}

func sampleHandler(w http.ResponseWriter, r *http.Request) {
  data := Example {
    Field: "test"
  }

  if err := parcel.RenderResponse(w, &data); err != nil {
    parcel.RenderErrorResponse(w, err)
  }
}

Render text to response


func sampleHandler(w http.ResponseWriter, r *http.Request) {
  data := "This will be rendered as text"

  // no need to use a pointer when rendering text
  if err := parcel.RenderResponse(w, data); err != nil {
    parcel.RenderErrorResponse(w, err)
  }
}

Create HTTP error and respond with it


func sampleHandler(w http.ResponseWriter, r *http.Request) {
  err := httperror.New(http.StatusNotFound, "Resource not found")

  // This will set the HTTP response to NotFound(404)
  parcel.RenderErrorResponse(w, err)
}

Wrap existing error with HTTP response


func sampleHandler(w http.ResponseWriter, r *http.Request) {
  err := functionThatFailed()

  // This will set the HTTP response to Conflict(409)
  parcel.RenderErrorResponse(w, httperror.Wrap(http.StatusConflict, err))
}

Render error not wrapped by parcel


func sampleHandler(w http.ResponseWriter, r *http.Request) {
  err := functionThatFailed()

  // This will set the HTTP response to InternalServerError(500)
  parcel.RenderErrorResponse(w, err)
}

Set Global Renderers and Binders

func setHandlers() {
  parcel.SetTextBinder(somepackage.Binder{})
  // This binder will only be used for requests with the mime type "application/sometype"
  parcel.SetObjectBinder("application/sometype", somepackage.Binder{})

  parcel.SetTextRenderer(somepackage.Renderer{})
  parcel.SetObjectRenderer(somepackage.Renderer{})
  parcel.SetErrorRenderer(somepackage.Renderer{})
}
Similar Resources

Httpx - a fast and multi-purpose HTTP toolkit allow to run multiple probers using retryablehttp library

Httpx - a fast and multi-purpose HTTP toolkit allow to run multiple probers using retryablehttp library

httpx is a fast and multi-purpose HTTP toolkit allow to run multiple probers using retryablehttp library, it is designed to maintain the result reliability with increased threads.

Feb 3, 2022

An HTTP proxy library for Go

Introduction Package goproxy provides a customizable HTTP proxy library for Go (golang), It supports regular HTTP proxy, HTTPS through CONNECT, and "h

Jan 4, 2023

Cake is a lightweight HTTP client library for GO, inspired by Java Open-Feign.

Cake is a lightweight HTTP client library for GO, inspired by Java Open-Feign. Installation # With Go Modules, recommanded with go version 1.16

Oct 6, 2022

This is a simple single-host reverse proxy that intercept and save HTTP requests and responses

This is a simple single-host reverse proxy that intercept and save HTTP requests and responses

HTTP Telescope Debug HTTP requests using a reverse proxy. Description This is a simple single-host reverse proxy that intercept and save HTTP requests

Mar 20, 2022

Goget will send a http request, and show the request time, status, response, and save response to a file

Goget will send a http request, and show the request time, status, response, and save response to a file

Feb 9, 2022

Go (golang) http calls with retries and backoff

pester pester wraps Go's standard lib http client to provide several options to increase resiliency in your request. If you experience poor network co

Dec 28, 2022

HTTP Load Testing And Benchmarking Tool

GBench HTTP Load Testing And Benchmarking Tool inspired by Apache Benchmark and Siege. Requirements You need Golang installed and ready on your system

Jan 2, 2020

HTTP/HTTPS load testing and benchmarking tool

Introduction I wrote that code because: (the obvious reason::I love to write code in Go) We are working so hard to optimize our servers - why shouldn'

Dec 5, 2022

Replacement of ApacheBench(ab), support for transactional requests, support for command line and package references to HTTP stress testing tool.

stress stress is an HTTP stress testing tool. Through this tool, you can do a stress test on the HTTP service and get detailed test results. It is ins

Aug 23, 2022
Related tags
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http

fasthttp Fast HTTP implementation for Go. Currently fasthttp is successfully used by VertaMedia in a production serving up to 200K rps from more than

Jan 2, 2023
Speak HTTP like a local. (the simple, intuitive HTTP console, golang version)

http-gonsole This is the Go port of the http-console. Speak HTTP like a local Talking to an HTTP server with curl can be fun, but most of the time it'

Jul 14, 2021
Http client call for golang http api calls

httpclient-call-go This library is used to make http calls to different API services Install Package go get

Oct 7, 2022
fhttp is a fork of net/http that provides an array of features pertaining to the fingerprint of the golang http client.

fhttp The f stands for flex. fhttp is a fork of net/http that provides an array of features pertaining to the fingerprint of the golang http client. T

Jan 1, 2023
NATS HTTP Round Tripper - This is a Golang http.RoundTripper that uses NATS as a transport.

This is a Golang http.RoundTripper that uses NATS as a transport. Included is a http.RoundTripper for clients, a server that uses normal HTTP Handlers and any existing http handler mux and a Caddy Server transport.

Dec 6, 2022
Simple HTTP package that wraps net/http

Simple HTTP package that wraps net/http

Jan 17, 2022
Http-conection - A simple example of how to establish a HTTP connection using Golang

A simple example of how to establish a HTTP connection using Golang

Feb 1, 2022
Simple HTTP and REST client library for Go

Resty Simple HTTP and REST client library for Go (inspired by Ruby rest-client) Features section describes in detail about Resty capabilities Resty Co

Jan 1, 2023
A Go HTTP client library for creating and sending API requests
A Go HTTP client library for creating and sending API requests

Sling Sling is a Go HTTP client library for creating and sending API requests. Slings store HTTP Request properties to simplify sending requests and d

Jan 7, 2023
httpreq is an http request library written with Golang to make requests and handle responses easily.

httpreq is an http request library written with Golang to make requests and handle responses easily. Install go get github.com/binalyze/http

Feb 10, 2022