Project

General

Profile

Is it possible to manage the status code for response?

Igor Khomenko
Added almost 4 years ago

Is it possible to manage the status code for response?

For example for POST request it returns 200 all the time.

Is it possible to set custom for example when a user provided invalid parameters?


Replies (6)

Added by Wojciech Kapcia TigaseTeam almost 4 years ago

Actually returning 200 response is mandated by the specification.

Added by Igor Khomenko almost 4 years ago

Do you mean that there is an XMPP specification for REST API?

I thought it's Tigase own solution

Added by Wojciech Kapcia TigaseTeam almost 4 years ago

Ah, sorry, I've missed that this was HTTP API forums and assumed it's about bosh. Please ignore my reply.

Added by Igor Khomenko almost 4 years ago

Any thoughts on this?

Added by Andrzej Wójcik IoT 1 CloudTigaseTeam almost 4 years ago

Yes, it is possible but it not very simple.

Normally in HTTP API components script in method responsible for processing you call callback function to pass results of your processing, ie.:

def results = [success:true];
...
callback(results);

but you call also pass Groovy's closure as a result. This closure needs to take two parameters.

If you return such closure then HTTP API will execute this closure and will pass instance of javax.servlet.http.HttpServletRequest as first parameter and instance of javax.servlet.http.HttpServletResponse as second parameter. This allows you to set almost any custom value including setting custom result code, ie.:

def c = { request, response ->
    response.sendError(404);
};
...
callback(c);

will send result as HTTP 404 error.

Added by Igor Khomenko almost 4 years ago

Great, thanks, it works!

would be great to have this in documentation as well

    (1-6/6)