Skip to main content

WriteJSONResponse

Writes a JSON to the http output stream

def WriteJSONResponse(
resp: http.ResponseWriter,
code: int,
body: interface{}
) - > error

Writes a JSON to the HTTP output stream. This function is used to send a JSON formatted response back to the client.

Parameters

NameTypeDescription
resphttp.ResponseWriterThe HTTP response writer to which the JSON response will be written.
codeintThe HTTP status code to set for the response (e.g., 200 for OK, 400 for Bad Request).
bodyinterface{}The data structure to be marshaled into JSON and sent as the response body.

Returns

TypeDescription
errorAn error if marshaling the body to JSON fails or if writing the string response encounters an issue.