How to define two separate structures for a single REST API Method?

How to define two separate structures for a single REST API Method?


I have two conditions,
1. Returns full records with separate structure.

2. Returns only error message if my condition fails (i.e. only {"Key" : "Value"}).

Now, How can I handle this?
I don't think that is possible. 

Why don't you return always a error variable? If no error has occured, just send an empty string.


If I understood your problem, I have a situation almost like yours. If not, sorry about that.

I can handle three differents structures actually. But, the structures have the same initial point (the root item).

1) {
  "event": {
    "type": "bill_paid",
    "created_at": "2015-12-23T17:01:06.670-02:00",
    "data": {...}

2)  {
  "event": {
    "type": "charge_created",
    "created_at": "2015-12-23T17:01:06.670-02:00",
    "charge": {...}

So, I have a structure created this way:


When I get the data from the web service, the empty json item is ignored by outsytems plataform.

Hi Adelar,

Thanks for your reply!

But my structures are different from other.

If I have data then I am getting full records. and If my condition doesn't match then I only get error message.

Web Services are exposed by someone else, I can't change them.
Hi Brajesh,

So you are using the Consume REST API, right? (when I read the first post it looked like you were trying to Expose it).
Most connectors in the forge have that pattern. Check for example the Twitter Connector.
Basically in the OnAfterResponse it checks if ther was an error code and deserializes the error using an extension. I recommend you to download ardoJSON component to do that deserialization (the extension used in the connectors is a copy of that).

Also, note that in this case it uses the session to store the error, but there are other smart ways to pass the information.
Like returning it as an Header or just transforming the ResponseText to have a json with superset structure with also an error attribute.

João Rosado