+ Start a Discussion
Patrick Mayer 4Patrick Mayer 4 

Using a login json to grab info

Http httpProtocol1 = new Http();
Http httpProtocol2 = new Http();
// Create HTTP request to send.
HttpRequest request1 = new HttpRequest();
HttpRequest request2 = new HttpRequest();
// Set the endpoint URL.
String endpoint1 = 'https://website.com/Login.json?user=patrick@website.com&pass=Secret';
String endpoint2 = 'https://website.com/LoginRequiredInfo.json';
request1.setEndPoint(endpoint1);
request2.setEndPoint(endpoint2);
// Set the HTTP verb to GET.
request1.setMethod('GET');
request2.setMethod('GET');
// Send the HTTP request and get the response.
// The response is in JSON format.
HttpResponse response1 = httpProtocol1.send(request1);
HttpResponse response2 = httpProtocol2.send(request2);
System.debug(response2.getBody());


Code is included for clarification, but essentially I am trying use the first json that creates a cookie that gives admin status to the second so that the info can be read. It works if I enter the 2 URLs in succession into a browser.

I am wondering if anyone has done anything like this and gotten it to work.

Best Answer chosen by Patrick Mayer 4
pconpcon
Not really knowing what the response from endpoint1 returns and how it's cookie information is set., I cannot answer this with absolute certainty.  However, I can say the reason why it works in your browser and not in Apex is because the cookie scope of these two requests is independent.  You will need to retrieve the information from the HttpResponse of the first request and using the data from that response, generate a new cookie header like:

request2.setHeader('cookie', 'key=value');

All Answers

pconpcon
Not really knowing what the response from endpoint1 returns and how it's cookie information is set., I cannot answer this with absolute certainty.  However, I can say the reason why it works in your browser and not in Apex is because the cookie scope of these two requests is independent.  You will need to retrieve the information from the HttpResponse of the first request and using the data from that response, generate a new cookie header like:

request2.setHeader('cookie', 'key=value');

This was selected as the best answer
pconpcon
theat should be 'Cookie' in the setHeader
pconpcon
If this solved your issue, please choose it as the correct answer so that the question will be moved out of the unanswered queue.
Patrick Mayer 4Patrick Mayer 4
Http httpProtocol1 = new Http();
Http httpProtocol2 = new Http();
// Create HTTP request to send.
HttpRequest request1 = new HttpRequest();
HttpRequest request2 = new HttpRequest();
// Set the endpoint URL.
String endpoint1 = 'https://website.com/Login.json?user=patrick@website.com&pass=Secret';
String endpoint2 = 'https://website.com/LoginRequiredInfo.json';
request1.setEndPoint(endpoint1);
request2.setEndPoint(endpoint2);
// Set the HTTP verbs
request1.setMethod('POST');
request2.setMethod('GET');
// Send the HTTP requests and get the responses
HttpResponse response1 = httpProtocol1.send(request1);
String cookie = response1.getHeader('Set-Cookie');
request2.setHeader('cookie', cookie);
HttpResponse response2 = httpProtocol2.send(request2);
Here is code for what ended up working thanks!