+ Start a Discussion
Prajapati.LakhanPrajapati.Lakhan 

Cookie is not getting created after Apex Callout

Hi Everyone,

 

I am trying to consume some apis in salesforce thier authentication approach is as follows -

 

When we send an HTTP login request to the given endpoint with the usename and password header following items are expected to return -
1. A session cookie, jsessionid. The client must use this cookie when submitting subsequent requests, including logoff requests.
2. A status code of 200, if the session does not encounter any errors. This indicates that the request succeeded.

 

The problem is that when I tried this api call using Apex callout, it returned status code of 200 but no cookie with the name 'jsessionid' is created. However i tried this API call using XMLHttpRequest object it works fine.

 

Please let me know if somebody knows the issue.

 

 

Thanks,

Lakhan

Best Answer chosen by Admin (Salesforce Developers) 
paul-lmipaul-lmi

you can't use the getCookies method because you're trying to access a cookie outside of Salesforce.  Instead, you need to iterate through the headers in the HTTP Callout's response in Apex, find the header you need, and then re-use that in subsequent HTTP callouts.  Apex does not support HTTP session management at all, so basically, you have to do it manually.

 

In the headers of the callout response, you'll find some headers like "set-cookie".  That's what you're looking for, and you need to add a header to your subsequent requests called "cookie" or "jsessionid" or whatever the target web server is expecting.

All Answers

Afzal MohammadAfzal Mohammad

Assuming that you are attempting the callout from a vf page, here is what you could do to check if cookie exists.

 

ApexPages.currentPage().getCookies().get('jsessionid');

 

Hope that helps.

 

Afzal

Prajapati.LakhanPrajapati.Lakhan

Yes, I am calling this request from Visualforce page and I tried it too but always getting null value for getCookie() method. Even I didnot find that cookie in browser's cookie folder. Does salesforce allow cookie in HTTPResponse header?

 

 

Thanks,

Lakhan

paul-lmipaul-lmi

you can't use the getCookies method because you're trying to access a cookie outside of Salesforce.  Instead, you need to iterate through the headers in the HTTP Callout's response in Apex, find the header you need, and then re-use that in subsequent HTTP callouts.  Apex does not support HTTP session management at all, so basically, you have to do it manually.

 

In the headers of the callout response, you'll find some headers like "set-cookie".  That's what you're looking for, and you need to add a header to your subsequent requests called "cookie" or "jsessionid" or whatever the target web server is expecting.

This was selected as the best answer
Prajapati.LakhanPrajapati.Lakhan

Thanks Paul!

 

It helped me to solve my problem.

 

 

Srilekhya23Srilekhya23
Hi,

I have a similar problem. Could you please explain your solution exactly how you solved.
I have set the cookie in the GET callout setheader(Cookie), but there is no cookie set in my browser.