httputil

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httputil contains a collection of HTTP request and response utility functions.

Index

Constants

View Source
const (
	// MimeApplicationJSON contains the mime type string for JSON content.
	MimeApplicationJSON = "application/json; charset=utf-8"

	// MimeTextPlain contains the mime type string for text content.
	MimeTextPlain = "text/plain; charset=utf-8"
)
View Source
const (
	StatusSuccess = "success"
	StatusFail    = "fail"
	StatusError   = "error"
)

JSend status codes.

Variables

This section is empty.

Functions

func AddBasicAuth

func AddBasicAuth(apiKey, apiSecret string, r *http.Request)

AddBasicAuth decorates the provided http.Request with Basic Authorization.

func HeaderOrDefault

func HeaderOrDefault(r *http.Request, key string, defaultValue string) string

HeaderOrDefault returns the value of an HTTP header or a default value.

func Link(url, template string, segments ...interface{}) string

Link generates a public Link using the service url. It replaces all segments into the template. The template then gets joined at the end of the service url.

func PathParam

func PathParam(r *http.Request, name string) string

PathParam returns the value from the named path segment.

func QueryIntOrDefault added in v1.3.0

func QueryIntOrDefault(q url.Values, key string, def int) int

QueryIntOrDefault set default value for a particular int key.

func QueryUintOrDefault added in v1.3.0

func QueryUintOrDefault(q url.Values, key string, def uint) uint

QueryUintOrDefault set default value for a particular uint key.

func SendJSON

func SendJSON(ctx context.Context, w http.ResponseWriter, statusCode int, data interface{})

SendJSON sends a JSON object to the response.

func SendStatus

func SendStatus(ctx context.Context, w http.ResponseWriter, statusCode int)

SendStatus sends write a HTTP status code to the response.

func SendText

func SendText(ctx context.Context, w http.ResponseWriter, statusCode int, data string)

SendText sends a JSON marshaled object to the response.

Types

type Status

type Status int

Status translates the HTTP status code to a JSend status string.

func (Status) MarshalJSON

func (sc Status) MarshalJSON() ([]byte, error)

MarshalJSON implements the custom marshaling function for the json encoder.

Directories

Path Synopsis
Package jsendx implements a custom JSEND model to wrap HTTP responses in a JSON object with default fields.
Package jsendx implements a custom JSEND model to wrap HTTP responses in a JSON object with default fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL