Question
· Nov 4, 2016

How to force HTTP status response using EnsLib.REST.Service

Hi everybody,

I created my own REST service class by extending   EnsLib.REST.Service.

In some particular conditions of  the parameters of the request,  the REST  service should respond to the client with an HTTP status response code 400 "Bad request".

I read the article "RESTful Exception Handling "  and I try to use the suggested:

do ..ReportHttpStatusCode(400)

But the server seems ignore it and  get back to client  the 500 http response code.

Any suggestions?

I am sorry if the question is a too low knowledge level but I am pretty newbie to Intersystems world.

Alberto

Discussion (4)1
Log in or sign up to continue

I have a class which extends EnsLib.REST.Service

and my URL map is as below

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap]
{
<Routes>
<Route Url="/test" Method="POST" Call="MyLib.Service.REST.TestRequestHandler:ProcessRequest"/>
</Routes>
}

-----------------

 

And the class contains

 

Class OCIELib.Service.REST.TestRequestHandler Extends %RegisteredObject
{

ClassMethod ProcessRequest(pStreamIn As %CharacterStream, Output pStreamOut As %Stream.Object) As %Status
{
// This method returns a test resonse that can be used to verify connectivity between client
// and OCIE REST endpoint.
Do pStreamOut.SetAttribute("Content-Type","application/text")
Do pStreamOut.Write("THIS IS A TEST RESPONSE FROM THE SYSTEM")

Quit $$$OK
}

}

Here calling ReportHttpStatusCode(..#HTTP400BADREQUEST, sc) does not work it seems. Is there any specific way to call the method.

 

I tried calling usinf ##class(%CSP.REST).ReportHttpStatusCode() it did not work..

Any help is appreciated.

When we are extedning EnsLib.REST.Service, if we set the response code in stream attribute it would send the reponse in that way.

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap]
{
<Routes>
<Route Url="/test" Method="POST" Call="MyLib.Service.REST.TestRequestHandler:ProcessRequest"/>
</Routes>
}

-----------------

And the class contains

Class OCIELib.Service.REST.TestRequestHandler Extends %RegisteredObject
{

ClassMethod ProcessRequest(pStreamIn As %CharacterStream, Output pStreamOut As %Stream.Object) As %Status
{
// This method returns a test resonse that can be used to verify connectivity between client
// and OCIE REST endpoint.
Do pStreamOut.SetAttribute("Content-Type","application/text")
Do pStreamOut.Write("THIS IS A TEST RESPONSE FROM THE SYSTEM")
do pStreamOut.SetAttribute("ResponseCode","400 Bad Request")
Quit $$$OK
}

Hope this helps. I was also stuck here , found the solution today itself.