1

I can't seem to pinpoint where the issue is in my Apex REST callout that's causing the following error to appear during execution:

Unexpected token '<EOF>'.

My code is as follows:

@RestResource(urlMapping='/*/')
global class AuthCallout {
    @HttpGet
    global static void basicAuthCallout(){
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('callout:/Sharepoint_Online/_layouts/15/sharepoint.aspx?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
        req.setHeader('APIKEY', '{!$Sharepoint_Online.Password}');
        //req.setCompressed(true);
        HTTPResponse res = new Http().send(req);
        Document doc = new Document(Name='test.txt', Body=res.getBodyAsBlob(), FolderId='xxxxxxxxxxxxxxx');
        insert doc;
    }
}
  • 3
    Is it a compile issue or do you get the issue while you're running the code? – gNerb Jun 5 '18 at 19:27
  • 1
    Not that I would expect it to cause the failure message you indicate, but I think the leading slash on your setEndpoint call is incorrect. I think it's supposed to be setEndpoint('callout:Named_Credential_Name/path/to/resource?param=value'). – Adrian Larson Jun 5 '18 at 19:36
  • It's an issue while running the code. I have '0' compile issues atm. – xorLogic Jun 5 '18 at 19:38
2

The issue is the

Body=res.getBodyAsBlob()

According to this, the body field is base64 not blob. Try

String body = EncodingUtil.base64Encode(res.getBodyAsBlob());
Document doc = new Document(Name='test.txt', Body=body), FolderId='xxxxxxxxxxxxxxx');

It's also a good idea to explicitly set the content type, use text/plain for .txt

Update

There are two issues, the other issue is the URL mapping. It has an extra / in it. It should be '/*'

If an '*' appears, it must be preceded by '/' and followed by '/', unless the '*' is the last character, in which case it need not be followed by '/' Source

  • I get a problem: "Illegal assignment from String to Blob" at the "String body = ..." line. Also is my URL Mapping sufficient at the top? I don't know how to frame it so I left it as a wildcard. – xorLogic Jun 5 '18 at 19:49
  • I'll need to see your code to figure out why you're getting that error, the code as I wrote it is as correct as I can make it. – gNerb Jun 5 '18 at 19:51
  • Ok. I came across this reference (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…) as well and tried to create a type Blob to store res.getbodyasblob(); but that didn't seem to work either. – xorLogic Jun 5 '18 at 19:55
  • try something like blob.valueOf(res.getBody()) – gNerb Jun 5 '18 at 20:10
  • 1
    According to this developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… You don't need the last / in your URL mapping, just '/*' – gNerb Jun 5 '18 at 21:05

Your Answer

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

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