Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Consider this code snippet from my answer to rao's question:

String requestUrl = '/services/data/v26.0/sobjects/User/describe';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + requestUrl);
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

HTTPResponse res = http.send(req);
String output = res.getBody();
System.debug(output);

Works like a charm (provided you've added the instance to "Remote Site Settings" of course).

Now try with standard Salesforce page, for example

String requestUrl = '/001/o'; // or '/home/home.jsp' or anything else

And you get back this weird "placeholder" regardless which page you'll call.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<script>
var escapedHash = '';
var url = 'https://test.salesforce.com/?ec=302&startURL=%2F001%2Fo';
if (window.location.hash) {
   escapedHash = '%23' + window.location.hash.slice(1);
}
if (window.location.replace){ 
window.location.replace(url + escapedHash);
} else {;
window.location.href = url + escapedHash;
} 
</script>
</head>
</html>

Any idea what to do to get to the actual page?

Pass some undocumented header? Go through the frontdoor.jsp like suggested on SO? Or maybe I should expose PageReference.getContent() as a REST service :D

share|improve this question

2 Answers 2

up vote 11 down vote accepted

Please don't screen-scrape - it's just about the most fragile integration you can imagine. With the release of the Analytics API, it's also now largely unnecessary

Having said that, the Authorization HTTP header only works with API pages. For web pages like /001/o or /home/home.jsp you need to set the sid cookie instead. For example,

String requestUrl = '/001/o';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + requestUrl);
req.setMethod('GET');
req.setHeader('Cookie','sid='+UserInfo.getSessionId()); 

HTTPResponse res = http.send(req);
String output = res.getBody();
System.debug(output);

Yields

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html class="ext-strict"><head><script type="text/javascript" src="/jslibrary/1351189248000/sfdc/JiffyStubs.js"></script>
<title>Accounts: Home ~ salesforce.com - Developer Edition</title>
<!-- LOTS MORE VALID PAGE DATA -->
share|improve this answer
3  
and of course, totally unsupported, there's an API for a reason. –  superfell Nov 22 '12 at 16:30
    
Yes - I meant to say "please don't screen-scrape - it's just about the most fragile integration you can imagine". In fact, I'll go back and edit that in right now! –  metadaddy Nov 22 '12 at 17:19
    
I know, I know, I've seen stackoverflow.com/questions/7841998/… and salesforce.stackexchange.com/questions/337/… But do you have better idea how to retrieve results of any report with apex? Ideally not only flat details but also grouped (subtotals or matrix)? Without metadata api, orgy of XML parsing and ugly dynamic SOQL generator? –  eyescream Nov 22 '12 at 18:14
    
@eyescream added link to Analytics API. –  metadaddy Oct 17 at 17:49

Amazing help, Pat(@metadaddy)... this has allowed me to create some marvelous personal developer toys, like a recursive Show All Dependencies!

However, like Simon(@superfell) said, this is not only horribly unsupported and incredibly hard to maintain, but the implementation of this will most likely occur through heart-sinking Pattern and Matcher use, or unimaginable String search methods... since the page most likely won't successfully load into a Dom.Document out-of-the-box.

However, if you can survive without having this be a headless process (and you're really a glutton for punishment), may I suggest approaching this inside a VisualForce page using JavaScript? DOM access will make whatever you're going to do SO much easier.... and after you conquer the hurdle of learning Salesforce page drawing patterns and naming conventions, you'll be in much better shape than from server side coding.

share|improve this answer
    
I needed "only" to scrape the results from running a report (I had to send csv attachment while standard "schedule report" embeds HTML and shows only first 2K rows). I agree it's ugly, unsupported etc... but if my code is in the answer salesforce.stackexchange.com/a/4688/799 JS won't help me, right? Especially that said JS would have to be cross domain, coming with its own set of headaches. –  eyescream Dec 7 '12 at 6:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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