I'm trying to post data from my org to a third party using a REST API they've provided for us. The process for this is:

  1. POST client_credentials (client_id and client_secret) to obtain access_token
  2. GET x_csrf_token using client_id, client_secret and access_token
  3. POST Salesforce data using access_token and x_csrf_token

The problem I am facing is that I must also provide a JSESSIONID cookie value. Using REST tools (PostMan) I'm able to capture and utilise this cookie, but in in APEX there is no way (via HttpResponse) to access cookie values. My script doesn't belong to a controller or VisualForce in any way, so I can't use the PageReference class methods for handling cookies.

Is there any way to capture cookies sent back by an Http.send(HttpRequest) callout?

Below is the callout for step 1, to obtain my access_token. It is also where I have to capture cookies - captured automatically by the magic of PostMan, but how do I do it in APEX?

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
    + '&client_secret=XXXXXXXXXXXXXXXX'
    + '&grant_type=client_credentials');

HttpResponse response = http.send(request);

JSONParser parser = JSON.createParser(response.getBody());
// Parsing omitted...
String accessToken = parser.nextToken();

System.debug('Access token headers: ' + response.getHeaderKeys());
// Contains Connection, Content-Length, Date, Content-Type
  • 1
    It looks like your question may have been answered in this question: salesforce.stackexchange.com/questions/24642/… – John Towers Jan 14 '19 at 14:40
  • Thanks John, but actually I looked at that post already. The answer there assumes the response returns a "Set-Cookie" header, but my response does not (I've added more details to OP). I wonder if that's just a convention (maybe I can ask my third-party to provide it?) – D. Salvadori Jan 14 '19 at 14:49

Your Answer

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

Browse other questions tagged or ask your own question.