I am working on a application where hitting a button in Salesforce user interface must redirect you to some third party web application session. Below is some sample code that I use to connect:

Httprequest request = new HttpRequest();
Http http = new Http();
String jsonString='{"login":"serviceusername","password":"passwordtologin","clientname":"101","clientappid":"202","clientappkey":"303"}';   
//Making call to external REST API for login
HttpResponse response = http.send(request);  
return new Pagereference ('https://someendpoint.com/inbox');

Even though my API call was successful and I got proper session Id in response, But loaded page still redirects me to login screen. As per their support team, API call loads some cookies for every API call and those cookies must be set at browser for successful login. How can I get cookies from above API call and set them in browser.

Edit: Login Response (Taken using postman)

Response Body:

  "response": "{\"time_zone\":\"\",\"session_token\":\"ca54d572095682dd25c437e934efe8c8\",\"user_login\":\"username@domain.com\",\"user_profile_img\":\"http://domainname.com/images/service/profile.jpg\",\"org_admin\":\"\",\"phone_mobile\":\"\",\"user_email\":\"useremail@domain.com\",\"success\":\"0\",\"user_org_id\":\"324\",\"user_id\":\"2505\"}"

Response Headers:

Cache-Control →private, max-age=0, must-revalidate
Connection →Keep-Alive
Content-Encoding →gzip
Content-Type →application/json; charset=utf-8
Date →Fri, 27 May 2016 06:08:02 GMT
ETag →"e2823b2602a9751f511082620816f906"
Proxy-Connection →Keep-Alive
Server →nginxi
Status →200
Transfer-Encoding →chunked
X-Runtime →3

Also there were 7 name/value pair of cookies in response.

  • Mukesh, Why are you redirecting to endpoint if you are getting session id in response, you can directly set the session id you got in response using req.setHeader('Cookie','sid='+sid); in further api calls. Note :- Cookie name may be different for you service, you need get that name from browser cookies after login into the external system. – SFIntegrator May 27 '16 at 5:45
  • @SFIntegrator Sorry for the confusion. I have used dummy URLs in above code. The redirected URL is some landing page that user see after login and not some endpoint. – Mukesh Verma May 27 '16 at 5:58
  • Can you post your response string here? – SFIntegrator May 27 '16 at 6:02
  • @SFIntegrator Please see improvements in question. I hope that makes it more clear. – Mukesh Verma May 27 '16 at 6:19
  • Ok, I am not sure, but you can try this Pagereference pg = new Pagereference ('someendpoint.com/inbox'); pg.getHeaders().put('Cookie', 'YourSessionCookieName=sessionId'); return pg; – SFIntegrator May 27 '16 at 6:26

It seems that you are getting proper response with login i.e. you are getting both session id and cookies. Now when you redirect user to landing page you should set both session ID with cookies to be able to successfully authenticate.

Here is how to read cookie from response header:

HttpResponse response = http.send(request); 
String my_cookies = response.getHeader('Set-Cookie'); //

here is how you can set cookie header while redirecting.

Cookie ck = new Cookie(my_cookies); //You will need more manipulations i.e. read each value form cookies and set in list of Cookies

PageReference pageRef = new PageReference('https://someendpoint.com/inbox'');
pageRef.setCookies(List<Cookie> ck);
return pageRef ;

More detailed example on how to read and write cookie values is available here. http://developer.force.com/cookbook/recipe/storing-form-field-values-with-the-apex-cookie-class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.