How to pass JSON body as part of a HTTP Status 404 Response message

404 Not Found

{
"error": {
 "errors": [
  {
   "domain": "global",
   "reason": "notFound",
   "message": "Not Found"
  }
 ],
 "code": 404,
 "message": "Not Found"
 }
}

I want to do something like the above sample from a Google Storage JSON API. I have a call to Write obj.%ToJSON() followed by return ..ReportHttpStatusCode(..#HTTP404NOTFOUND) however the HTTP Status code is always 200.  If I remove the Write obj.%ToJSON() statement it returns a 404 status with no body. How do I return both?

  • 0
  • 0
  • 397
  • 0
  • 1

Answers

The Write obj.%ToJSON() writes to the TCP stream of the HTTP Response, the headers are written before the body.

You' have to call the ReportHttpStatusCode first (which is nothing more then:  Set %response.Status=HttpStatus) and then write the body.