UserIsStuck
UserIsStuck

Reputation: 529

SalesForce Apex code fails on file download - Premature EOF

In my Apex code, I am trying to download a remote csv file to process in-memory. Unfortunately, I am getting a System.CalloutException: Premature EOF error when I try to get the file. I am able to connect to the server with the file (I can see the error messages returned when the file is not ready for download) so the connection details are likely not the problem.

private static void processURL(String url, UserHelper__c helper){

String username = 'login';
String password = 'password';

HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();

req.setEndpoint(url);

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);

req.setMethod('GET');

try {
    System.debug('processURL Send request: '+req);
    res = http.send(req); //Premature EOF hits here.
    System.debug('processURL successful');

} catch(System.CalloutException e) {
    System.debug('processURL error: '+ e);
}

...

}

Upvotes: 1

Views: 1816

Answers (1)

UserIsStuck
UserIsStuck

Reputation: 529

This was caused by the server sending its response in a buffered manner. It seems that the SalesForce Apex code cannot handle a buffered response. I don't have all the details on what happened on the server-side so I cannot give more details than that.

Upvotes: 1

Related Questions