Documentation ¶
Overview ¶
Description: This package provides utility functions for writing JSON responses and checking mandatory query parameters.
Index ¶
- func CheckMandatoryQueryParams(queryParams map[string][]string, mandatoryQueryParams ...*structcup.QueryParam) error
- func WriteErrorResponse(w http.ResponseWriter, errorCode int, escapeHTML bool)
- func WriteGzipErrorResponse(w http.ResponseWriter, errorCode int, escapeHTML bool)
- func WriteGzipJSONResponse(w http.ResponseWriter, data interface{}, code int, escapeHTML bool) error
- func WriteJSONResponse(w http.ResponseWriter, data interface{}, code int, escapeHTML bool) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckMandatoryQueryParams ¶
func CheckMandatoryQueryParams(queryParams map[string][]string, mandatoryQueryParams ...*structcup.QueryParam) error
CheckMandatoryQueryParams checks if the mandatory query parameters are present in the request.
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 ¶
This section is empty.