The user does not need to set the response content type to send JSON as a response.
If the response content type is not set that means it is application/JSON.
If ContentType is set to below system will return data accordingly.
JSON = 'application/json'
XML = 'text/xml'
YAML = 'text/yaml'
TEXT = 'text/plain'
HTML = 'text/html'
Note: If content type is set and not from above list, then API Maker will consider a text the code returns as base64 encoded. Bytes the code returns (a Buffer, bytes, byte[], []byte…) are sent as they are, whatever the content type : see OCTET_STREAM below.
g.Res.ContentType="application/json";g.Res["errors"]=newJsonArray(newJsonObject{["code"]=401,["message"]="This is error"});g.Res.StatusCode=555;return"World";
g.res.set_content_type("application/json");*g.res.errors_mut()=json!([{"code":401,"message":"This is error"}]);g.res.set_status_code(555);Ok(json!("World"))
g.res.setContentType("application/json");g.res.setErrors(List.of(newResponseError(Map.of("code",401,"message","This is error"))));g.res.setStatusCode(555);return"World";
It will send the bytes as they are : a file the code read or downloaded, a PDF it made. Return the bytes (a Buffer, bytes, byte[], []byte), or leave them in g.res.output : the answer is those bytes, under the content type the code names, whatever it is (image/png, application/pdf…). Headers of the code go with them (Content-Disposition, Cache-Control).
// A text of PHP holds bytes too : AmBytes says that these are bytes.$picture = base64_decode('R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=');$g->res->contentType = 'image/gif';$g->res->headers = ['Content-Disposition' => 'inline; filename="pixel.gif"'];return new AmBytes($picture);
// The bytes of the picture, as they are : a value of JSON has nothing which stands for bytes, `am::bytes` does.letpicture=b"GIF89a\x01\0\x01\0\x80\0\0\xff\xff\xff\xff\xff\xff!\xf9\x04\x01\n\0\x01\0,\0\0\0\0\x01\0\x01\0\0\x02\x02L\x01\0;\0";g.res.set_content_type("image/gif");g.res.set_headers(json!({"Content-Disposition":"inline; filename=\"pixel.gif\""}));Ok(am::bytes(picture))