apijuice

package
v1.16.6 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Description: This package provides utility functions for writing JSON responses and checking mandatory query parameters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckMandatoryQueryParams

func CheckMandatoryQueryParams(queryParams map[string][]string, mandatoryQueryParams ...*QueryParam) error

CheckMandatoryQueryParams checks if the mandatory query parameters are present in the request.

func ServeHTML added in v1.14.11

func ServeHTML(w http.ResponseWriter, htmlContent io.Reader, code int)

ServeHTML writes an HTML response to the provided http.ResponseWriter with the given HTML content and HTTP status code.

Parameters:

  • w: http.ResponseWriter to write the response to.
  • htmlContent: HTML content to be sent as the response payload.
  • code: HTTP status code to be set in the response.

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, errorCode int, escapeHTML bool)

WriteErrorResponse writes an error response to the response writer with appropriate headers.

It does NOT return an error because it is used to write an error response.

Instead, if an error occurs internally, it writes the error to the response writer with http.Error.

func WriteGzipErrorResponse

func WriteGzipErrorResponse(w http.ResponseWriter, errorCode int, escapeHTML bool)

WriteGzipErrorResponse writes an error response to the response writer with appropriate headers.

It does NOT return an error because it is used to write an error response.

Instead, if an error occurs internally, it writes the error to the response writer with http.Error.

This method does NOT check for the Accept-Encoding header.

This method overrides the Content-Length header with the length of the compressed data.

Use this method only if you are sure that the client can handle gzip compression.

func WriteGzipJSONResponse

func WriteGzipJSONResponse(w http.ResponseWriter, data interface{}, code int, escapeHTML bool) error

WriteGzipJSONResponse compresses the JSON data and writes it to the response writer with appropriate headers.

The Content-Encoding header is set to gzip and the Content-Type header is set to application/json.

The Content-Length header is set with the length of the compressed data.

The status code is set based on the provided code parameter.

If the data cannot be serialized to JSON, an error is returned.

If the compressed data cannot be written to the response writer, an error is returned.

This method does NOT check for the Accept-Encoding header.

This method overrides the Content-Length header with the length of the compressed data.

Use this method only if you are sure that the client can handle gzip compression.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, data interface{}, code int, escapeHTML bool) error

WriteJSONResponse writes the JSON data to the response writer with appropriate headers.

The Content-Type header is set to application/json.

The Content-Length header is set with the length of the uncompressed data.

The Content-Lenght is not set if the Content-Encoding header is set to gzip.

The status code is set based on the provided code parameter.

If the data cannot be serialized to JSON, an error is returned.

If the uncompressed data cannot be written to the response writer, an error is returned.

Types

type QueryParam added in v1.15.12

type QueryParam struct {
	Key                   string
	Value                 string
	MultipleValuesAllowed bool
}

QueryParam represents a query parameter.

It contains a boolean to indicate if multiple values are allowed for a same key.

Jump to

Keyboard shortcuts

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