SOE REST Service - Is it possible to send the http status code from the operation

5291
6
07-30-2010 08:24 AM
PrashantKhanal
New Contributor
Hi Folks,

I was wondering if it is possible to send the status code to the client from the SOE operation. I know we can set the Content-Type header and also the ResponseDataType on the "responseProperties". Does the "responseProperties" takes other parameters as well such as http status code?
0 Kudos
6 Replies
RahulRavikumar
New Contributor
Sending HTTP status codes can get tricky. If you are using a JavaScript client, for JSONP callbacks to work you MUST always send a status of 200.

This is why REST API sends a status code of 200 even when your request failed, but adds the actual HTTP status in a error JSON object. You can choose to do the same.

Otherwise you can set a custom HTTP header using responseProperties.
0 Kudos
PrashantKhanal
New Contributor
Sending HTTP status codes can get tricky. If you are using a JavaScript client, for JSONP callbacks to work you MUST always send a status of 200.

This is why REST API sends a status code of 200 even when your request failed, but adds the actual HTTP status in a error JSON object. You can choose to do the same.

Otherwise you can set a custom HTTP header using responseProperties.


Thanks for the reply Rahul
I doing the same as you suggested for JSON response. The question came in my mind when i had to deal with image response. Say the requested output format is img as ...?f=img. Now when the request failed or some exception occurs in the operation, I thought sending a proper Http status code would be better as the client won't be expecting json response, sending json response wrapping actual status code won't make any sense. Correct me if I am wrong.

Could you tell how can i do that using responseProperties?
0 Kudos
RahulRavikumar
New Contributor
If there was a genuine exception, then you should just throw the appropriate exception. The REST handler intercepts the exception and sets the HTTP status to 500 (server error) with the appropriate exception message.

If you want to set custom HTTP headers you can use 'responseProperties'. If your SOE is in Java, then all you need to do is:

JSONObject jobj = new JSONObject();
jobj.put("X-HTTP-Status", "500, An error occured when generating the image.");
responseProperties[0] = jobj.toString();
0 Kudos
RahulRavikumar
New Contributor
If your SOE is in .NET you will have to construct the jsonified string yourself.

Essentially it would be:


responseProperties = "{
  \"Content-Type\" : \"image/png\",
  \"X-HTTP-Status\" : \"500 - An error occurred while generating the image.\"
}";

0 Kudos
PrashantKhanal
New Contributor
If your SOE is in .NET you will have to construct the jsonified string yourself.

Essentially it would be:


responseProperties = "{
  \"Content-Type\" : \"image/png\",
  \"X-HTTP-Status\" : \"500 - An error occurred while generating the image.\"
}";



Thanks Rahul.
X-HTTP-Status is not a standard header to my understanding. It should be an experimental header and do you mean the client should be able to interpret this header?

I tried to create a sample operation that constructs the response properties in the way you suggested but somehow I am not able to make it work.

RestOperation findNearFeatsOp = new RestOperation("GetImage",
                                                      new string[0] ,
                                                      new string[] { "img" },
                                                      GetImage);
..................

private byte[] GetImage(System.Collections.Specialized.NameValueCollection boundVariables, 
            ESRI.ArcGIS.SOESupport.JsonObject operationInput, 
            string outputFormat, 
            string requestProperties, 
            out string responseProperties)
        {
            responseProperties = "{\"Content-Type\" : \"image/png\",\"X-HTTP-Status\" : \"404 - Could not find the image requested.\"}";

            return null;
        }


Once error is encountered, I add X-HTTP-Status header and return null. I returned because I have nothing to return to the client. I guess that particular return null statement is causing the problem. I am confused what to return in such case. In case of json error, we just wrap the error in json and send the json string as bytes.
0 Kudos
RahulRavikumar
New Contributor
What errors are you getting when you return null?

When you set custom HTTP headers, you have to handle them by looking for custom HTTP headers yourself from the client code.

If you want the application to return an error message, then it should be HTTP status 500 (internal server error | error processing request). All you need to do for that, is to throw an Exception with a message, and the REST handler will intercept the exception and treat it as a 500.

So,

private byte[] GetImage(System.Collections.Specialized.NameValueCollection boundVariables, 
            ESRI.ArcGIS.SOESupport.JsonObject operationInput, 
            string outputFormat, 
            string requestProperties, 
            out string responseProperties)
        {
           try {
              ........
           }catch(Exception e) {
             //Log your exception and throw it so that the REST handler intercepts your exception
            // and treats it as a HTTP 500.
             throw e;
          }
        }
0 Kudos