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:
- POST client_credentials (client_id and client_secret) to obtain access_token
- GET x_csrf_token using client_id, client_secret and access_token
- 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.setEndpoint('https://myEndpoint/GenerateToken'); request.setMethod('POST'); request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); request.setBody('client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + '&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