Parameters

type string MIME type, e.g. application/json or text/html;charset=utf-8

Examples

Sets the Content-Type header of the response. Call it first, before writing the body: without it some clients (e.g. Restlet) fail to render the page, and a JSON API returned as text/html is mishandled by callers.

It is a method of the 📕 response object, available in JSSP pages and web apps.

// Return JSON
const resultJson = { abc: 123, def: 456 };
response.setContentType("application/json");
document.write(JSON.stringify(resultJson)); // { abc: 123, def: 456 }

// Return XML
const resultXml = new DOMDocument("abc");
response.setContentType("application/xml");
document.write(resultXml.toXMLString()); // <abc/>

Set it once per page. A JSON endpoint uses application/json; an HTML page uses text/html;charset=utf-8 to declare the charset.

See also

  • 📕 response - The HttpServletResponse object — set the content type, headers, cookies and redirects in a JSSP.
  • 📕 write - Append a string to the JSSP response body — the server-side equivalent of print.