Reading cookies in Mule ESB
If Mulesoft is good in anything it’s the lack of examples and documentation. I find myself busy with trial and error programming quite often. Now: reading a cookie from a HTTP request from an HTTP connector. So what I do is sending out an HTTP-request and get a response, which contains a cookie (e.g. session id). I have to use the value of the cookie in all the following request I am making to the endpoint.
Eventually it turned out to be pretty easy, only a few lines of code. But it took me some time figuring out whether I could do it with MEL or should do it in a custom transformer. And it wasn’t really clear how to access the cookie.
Basically you have to use the Mule library org.mule.transport.http.CookieHelper. You can obtain the cookie object by getting the inbound property Set-Cookie and then passing that as cookieobject in getCookieValueFromCookies. That method will then return the cookievalue as a string.
package robfox.transformers; import org.mule.api.MuleMessage; import org.mule.api.transformer.TransformerException; import org.mule.transformer.AbstractMessageTransformer; import org.mule.transport.http.CookieHelper; public class CookieTransformer extends AbstractMessageTransformer { public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { // Get all cookies from the header Object cookieObj = message.getInboundProperty("Set-Cookie"); // Get the value by calling using the CookieHelper String cookieVal = CookieHelper.getCookieValueFromCookies(cookieObj, "cookiname"); // Set a flow variable with the cookie's value, so we can use it later message.setInvocationProperty("flowvarname", cookieVal); return message; } }
Hi,
I tried this but I get the follow stacktrace:
org.mule.DefaultMuleMessage: setProperty(key, value) called with null value; removing key: flowvarname; please report the following stack trace to dev@mule.codehaus.org
java.lang.Throwable
at org.mule.DefaultMuleMessage.setProperty(DefaultMuleMessage.java:470)
at org.mule.DefaultMuleMessage.setInvocationProperty(DefaultMuleMessage.java:440)
at robfox.transformers.CookieTransformer.transformMessage(CookieTransformer.java:20)
at org.mule.transformer.AbstractMessageTransformer.transform(AbstractMessageTransformer.java:141)
at org.mule.transformer.AbstractMessageTransformer.transform(AbstractMessageTransformer.java:69)
at org.mule.transformer.AbstractTransformer.transform(AbstractTransformer.java:370)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mule.model.resolvers.AbstractEntryPointResolver.invokeMethod(AbstractEntryPointResolver.java:148)
at org.mule.model.resolvers.ReflectionEntryPointResolver.invoke(ReflectionEntryPointResolver.java:169)
at org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:36)
at org.mule.component.DefaultComponentLifecycleAdapter.invoke(DefaultComponentLifecycleAdapter.java:339)
at org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:82)
at org.mule.component.AbstractJavaComponent.doInvoke(AbstractJavaComponent.java:73)
at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:122)
at org.mule.component.AbstractComponent.access$000(AbstractComponent.java:57)
at org.mule.component.AbstractComponent$1$1.process(AbstractComponent.java:238)
at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:58)
at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
at org.mule.processor.chain.DefaultMessageProcessorChain.doProcess(DefaultMessageProcessorChain.java:94)
at org.mule.processor.chain.AbstractMessageProcessorChain.process(AbstractMessageProcessorChain.java:67)
Can you tell me why this occurs?
Thanks in advance!
Most likely the cookieVal contains NULL. This can happen if there’s no cookie with the name provided (cookiname).
I would suggest putting a breakpoint on line 17 and inspect the cookieObj whilst debugging. There you will be able to see whether the cookie exists or not in the object.
thanks for the assist. Worked perfectly.
Just found out in certain Mule versions this does not work anymore with the new HTTP connector. message.getInboundProperty(“Set-Cookie”); returns only the first cookie, instead of an array. This is solved in Mule 3.6.3, 3.8.0 M1 and Mule 3.7.3. See https://www.mulesoft.org/jira/browse/MULE-8790.