Use information from REST response headers in normal flow

Use information from REST response headers in normal flow

  

Hello, 

I'm consuming a REST service in my web application, but I need some information from a particular response header in my flow. For example: I can post an object as JSON to the API and the API responds with a header which contains the ID of the created object. I need that ID for further processing.

I tried the "OnAfterRequest" callback, but there I can only SEE the headers, I can't do anything with it in my normal flow. 

The only "solution" I can think of now is to create a custom exception, then raise that exception in the OnAfterRequest callback and send the ID from the header as ExceptionMessage but that is not how it supposed to work of course.

Any advice?

Hi Joeri,

In the "OnAfterRequest" you can also change the Response and then process it in the normal flow of the REST method.

Does that work for you?

Cheers,

José

Unfortunately no. The response type in the OnAfterRequest is of type HTTPResponse, and the response type of the API method is from another (custom) type. If I want to set the response type of the method to HTTPResponse, then it won't allow me to and gives an error saying that this type is reserved for API callbacks only.

Hi Joeri,

One of the attributes of the Response is the "ResponseText".

That is the response in json format and you change that to suit your needs.

Cheers,

José

That didn't work either :(

I don't know if I understand the solution, since it seems a bit off to me because I am working with different response types. I will show you my example:

Here you can see my REST method with its response type which I have access to in my normal flow.

And this is how the response looks like in the OnAfterResponse callback. Sure I can assign what I want to the ResponseText, but then where can I access that in my respons since there I only have "Origin", "Code", and "Message"

What do I need to change?

Solution

Hi Joeri,

I was suggesting something like this:

Basically:

  • Deserialize the CustomizedResponse.ResponseText
  • Change the attributes of that output
  • Serialize again
  • Assign that to the CustomizedResponse.ResponseText

Cheers,

José

Solution

Hi Joeri,

What I have done in the past is to manually add Attributes to the response, that are never actually sent by the REST API I'm consuming. In the OnAfterResponse, I used the JSON Deserialize Statement to convert the ResponseText to the right Structure, then fill the additional Attributes (which will always be empty after the Deserialize), then use JSON Serialize to convert it back and assign that to the CustomizedResponse.

Not the nicest of ways, but it kinda works.

EDIT: Ninja'd by José :)

Okay, now it kind of works (thanks José and Kilian). I can see in the debugger that it assigns the value from the header to the custom response but then afterwards it doesn't return to its normal flow. It seems that it triggers an exception after the "OnAfterResponse" callback but the exception is completely empty (no message, no code or whatever).

Any clue why this is happening?

Joeri,

Make sure you first assign the full Response Structure to CustomizedResponse, in order to not overwrite the StatusCode (a StatusCode outside the 200-range will cause an exception).

Yes, of course haha, I forgot. 

It works now, thanks guys!

EDIT: Unfortunately I can't mark both of you as the solution :(

You're welcome Joeri! Happy coding :)