# Content Types Example

> Explore how different response content types like JSON, HTML, XML, YAML, text, and binary work in API Maker with practical examples.

Source: https://docs.apimaker.dev/v1/examples/res/contentType/contentType.html

- 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.
```text
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**

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

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

## JSON

**Content type JSON**

```typescript
g.res.contentType = T.EContentType.JSON;
g.res.errors = [{ code: 401, message: "This is error" }];
g.res.statusCode = 555;
return "World";
```

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

### OCTET_STREAM

**Content type OCTET_STREAM**

```typescript
g.res.contentType = T.EContentType.OCTET_STREAM;
```

- It will download the image.

**base64**

```typescript
g.res.contentType = <any>'image/gif';
return `data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOwA=`
```

### TEXT

**Content type TEXT**

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

Response
```text
Hello world from the API Maker.
```

### XML

**Content type XML**

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

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

### YAML

**Content type YAML**

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

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