google-apps-scripthttp-status-codes

In Google Apps Script, how can I set the HTTP response code for a service I implement?


Say I have "service" returning a XML document:

function doGet() {
    var result = '<result>42</result>';
    var output = ContentService.createTextOutput(result);
    output.setMimeType(ContentService.MimeType.XML);
    return output;
}

By default, the HTTP status code for the response will be 200. How can I set it something different, say 500 (while still returning the same XML document)?


Solution

  • This is not currently possible with the ContentService. Implicitly, only the 200 status code can be returned back by the successfully completion of the script. Other error codes are all system level that get raised when something goes wrong.

    In 2013, an issue was filed to allow sending custom status codes. As of October 2024, this feature has not been implemented.