Skip to content

Content Types

  • 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 it as base64 encoded.

HTML

Content type HTML
g.res.contentType = T.EContentType.HTML;
return "<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>";

Response

<!DOCTYPE html><html><body><h1>My First Heading </h1><p> My first paragraph.</p></body></html>

JSON

Content type JSON
1
2
3
4
g.res.contentType = T.EContentType.JSON;
g.res.errors = [{ code: 401, message: "This is error" }];
g.res.statusCode = 555;
return "World";

Response

{
    "success": false,
    "statusCode": 555,
    "data": "World",
    "errors": [
        {
            "code": 401,
            "message": "This is error"
        }
    ],
    "warnings": []
}

OCTET_STREAM

Content type OCTET_STREAM
g.res.contentType = T.EContentType.OCTET_STREAM;
  • It will download the image.
    base64
    g.res.contentType = <any>'image/gif';
    return `data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=`
    

TEXT

Content type TEXT
g.res.contentType = T.EContentType.TEXT;
return "Hello world from the API Maker.";

Response

Hello world from the API Maker.

XML

Content type XML
g.res.contentType = T.EContentType.XML;
return "Hello world from the API Maker.";

Response

<?xml version='1.0'?>
<root>
    <success>true</success>
    <statusCode>200</statusCode>
    <data>Hello world from the API Maker.</data>
</root>

YAML

Content type YAML
g.res.contentType = T.EContentType.YAML;
return "Hello world from the API Maker.";

Response

success: true
statusCode: 200
data: Hello world from the API Maker.