Search code examples
salesforceattachmentapexzendesk

Get Zendesk attachment as base64 instead auto download


I need to get attachment file from zendesk in base64 format.

I try to do this but it's not worked for me:

`HttpRequest req = new HttpRequest(); req.setEndpoint('https://-----.zendesk.com/attachments/token/TOKEN/?name=stvm+%2810%29.pdf'); req.setMethod('GET');

Http http = new Http(); HTTPResponse res = http.send(req);

Blob body= res.getBodyAsBlob();`

But the body value it's working as i need.

The response it's something like that:

<html><body>You are being <a href="https://xxx.zdusercontent.com/attachment/000001/sdlfkashdf98709udfah?token=eyJhbGciOiJkaX46SgYrFzTEpYqUIzpQeNnl5BMBNoRnUOsgQj389Ei7nNcGOcfGYaavlqLL2qaIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..U8oX8QnYBM1lZMb6rhQGRA.NC3Z9kHC9ZE6HhygIHHan6xWYvoPqziVx76CZ6vcNYHBuAjV-LmBclVJYumKWKXA_PDhXX27z977XKYrLJZSc85a6lJTEqd-V2mP7U6O6r0_6E9hO8CWaA1dyxYYWw8kUsgMFUaPr0wCupxm3NbDzT03ZwO6EBJj79x4UZdauiXfEUrSwdl1pPahlQE2VfFo8DprgX9GQHzRFm5lwMrhA3crogo8Ox

Thanks!


Solution

  • I found a resolution for my problem.

    In salesforce apex code I got the URL response by substring method and I used Pagereferece to open the URL, see below:

    String exampleMyResponse= '<html><body>You are being <a href="https://xxx.zdusercontent.com/attachment/000001/sdlfkashdf98709udfah?token=eyJhbGciOiJkaX46SgYrFzTEpYqUIzpQeNnl5BMBNoRnUOsgQj389Ei7nNcGOcfGYaavlqLL2qaIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..U8oX8QnYBM1lZMb6rhQGRA.NC3Z9kHC9ZE6HhygIHHan6xWYvoPqziVx76CZ6vcNYHBuAjV-LmBclVJYumKWKXA_PDhXX27z977XKYrLJZSc85a6lJTEqd-V2mP7U6O6r0_6E9hO8CWaA1dyxYYWw8kUsgMFUaPr0wCupxm3NbDzT03ZwO6EBJj79x4UZdauiXfEUrSwdl1pPahlQE2VfFo8DprgX9GQHzRFm5lwMrhA3crogo8Ox';
    
    //You need to authorize your domain "https://xxx.zdusercontent.com" in remote site
    Pagereference pg = new Pagereference(exampleMyResponse.substringAfter('href="')); 
    Blob b = pg.getContentAsPDF();
        
    Attachment att = new Attachment(Name = 'stvm_4', Body = b, ContentType = 'application/pdf', ParentId='Sobject_Id');
    insert att;