function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
venkateshpvenkateshp 

Sending images from salesforce using apex rest api to mobile clinets

IN one project, we are provinding webservices from salesforce APEX REST API, we are sending data from salesforce to Mobile clients successfully, i need to send the images also , which are stored in the notes and attachments related to the record. But rest response not allowed to sendme the  blob content(Attachment body). Is there any work around to send images from salesforce through apexRest to mobile clinets.

 

 

Its pretty urgent to me

 

 

Thanks in advance!

 

Warm Regards

Venkatesh

SuperfellSuperfell

Your response can be a blob, its just that the autoamatic serialization doesn't work for blobs, but you can still write the blob to the responseBody, e.g.

 

@RestResource(UrlMapping='/foo')
global class BlobTest {

    @HttpGet
    global static void getBlob() {
        Attachment a = [select body from attachment where id='00P3000000CEhwHEAT'];
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type','image/jpg');
        res.responseBody = a.body;
    }
}

 

This returns the blob as the response, e.g. using curl you can fetch the blob and store it with

curl -v -H "Authorization: OAuth $SID" https://na1.salesforce.com/services/apexrest/foo -o foo.jpg

 

venkateshpvenkateshp

Hi Simon,

 

Thank you so much for quick response

 

i am trying to customize the json response while sending to cleint like below

 



@RestResource(urlMapping='/Account/*')
global with sharing class account{

@HttpGet
global static List<accountParser> doGet(){

RestRequest req = RestContext.request;
String id=ree.params.get('id');
RestResponse res = RestContext.response;
res.addHeader('Content-Type','image/jpg');

list<Accountparser> accParser=new List<Accountparser>();

Account acc=[select name,id  from account where name];

Attachment att=[select id ,body from Attachment where parentId=acc.id];

Accountparser ac= new Accountparser(acc.id,acc.name,att.body);

accParser.add(ac);
return acc;
}

}


global class Accountparser{

String id;
string name;
blob body;

global AccountParser(String id, String name,Blob body){

this.id=id;
this.name=name;
this.body=body;

}

but getting error like

Error: Compile Error: Invalid type for Http* method: LIST.LoginPraser.Blob

 

without blob content the code working fine. Please help me

 

 

 

Thank you in advance

 

Venkatesh

SuperfellSuperfell

You can not return json data & binary data at the same time, it would need to be 2 separate resources. (or you need to base64 encode the binary data and retun that as a string within your json, not recommended)

venkateshpvenkateshp

Hi Simon,

 

1.can i call two resources at a single call request

 

2. If i do base64 encode to the binary data as a string using EncodeUtils.encodeBase64(blobdata.'UTF-8')

 

how the client app  will retrive image from thsi string , what decription techniques need to client app follow.

 

 

Warm Regards,

 

Venkatesh