Thursday, June 20, 2013

DataPower: Setting the Content-Type header in an Error Rule

I learned a new trick today. This has been haunting me for many months now but I've managed to avoid implementing it for my customer. Today, being pressured by deadlines, I learned a cool new trick with DataPower: setting the Content-Type in an error rule.

The reason why this has eluded me for sometime is that no matter how much effort I do setting it inside an XSL in a transform action using either <dp:set-http-response-header name="'Content-Type'" value="'application/json'" /> or <dp:set-response-header name="'Content-Type'" value="'application/json'" /> and even if I do a <dp:freeze-headers /> after that, it still didn't work. I've also tried doing a set-var action setting "var://service/set-response-header/content-type" to application/json and placing it before the result action, it still does not work (this was what most of my Google search said to do). And I've been trying and trying every now and then to make it work but to no avail.

Finally, this afternoon, pressured to meet a tight deadline, I did one final Google search and found a link that I haven't read before simply because it did not exist prior to June 19, 2013. I tried it out and it worked perfectly!

How it was done was quite unusual actually, I got the part where you specify the value of var://service/set-response-header/content-type right but what I did wrong was were I placed it. Prior to June 19, my quick searches say that the set var action should be placed before the result action. But the link to a developerWorks forum post says that it should be set AFTER the result action.



When I tried it out for myself, I jumped in complete joy (the geek side of me kicking-in) and amazement. I was like: WOW! Finally!

5 comments:

Anonymous said...

You are a life saver man!!.

Noel Avlas said...

Thank you! Glad to be of help.

Anonymous said...

Thank you for this post . However I have a query . Could you please let me know your thoughts on this .

Can we send back JSON and SOAP errors along a single error rule with the right content-type ?

Noel Avlas said...

Hi, yes you can (if my memory serves me right). I think I have implemented this before.

What you can do is in a transform action in your error rule, do a logic which identifies whether the error would be JSON or SOAP and the assign the content type to a variable.

Then, inside your set variable action, specify your content type variable in the variable assignment.

Forgive me if I'm mistaken, this is all running in my head. I haven't done DataPower in almost a year.

celso nagata said...

Muito obrigado cara!
Translating: Thank you very much man!!