19

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)?

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Got it. I imagine this can be useful every time the code doesn't "blow up", but instead detects an error which warrants a 500 response code. Being able to set the response code is commonly a part of the API for a "response" class, in fact so common that I am not sure this warrants the description of a use case ;).
@mzimmerman I tried throwing an exception, but that still returns a 200 and some html text. Not super useful when the XML document is being consumed by a service.
Wow, this is still the correct answer today :-( (The feature request has been migrated to issuetracker.google.com/issues/36759757)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.