You need to sign in to do that
Don't have an account?
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
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
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
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
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.
Thanks Paul!
It helped me to solve my problem.
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.