+ Start a Discussion
SijuSiju 

How to send a POST HttpRequest with Image and data

I am trying to send an Image and some details to external system (REST JSON). I added the image like this mapEmp.put('image',att.body);   It is not working and throwing error  but  If I remove that line ,its working fine with out image.Please  check it let meknow I am missing anything or approch is wrong?

 HttpRequest req = new HttpRequest();
        Attachment att = new Attachment();
       att = [SELECT ID,Body,ContentType FROM Attachment where ContentType='image/jpeg' and ID='00PD000008GcfohMAB' LIMIT 1];
        req.setEndpoint('http://abc');
        req.setMethod('POST');
        String username = 'abc@xxx.org';
        String password = 'password1';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);      
        req.setHeader('Content-Type', 'multipart/form-data');
        req.setHeader('Content-Type', 'application/json');     
        system.debug('Auth Header: ' + authorizationHeader);
        Map<String,object> mapEmp = new Map<String,object>();
        mapEmp.put('title', title);
        mapEmp.put('authorDisplayName', authorDisplayName);
        mapEmp.put('lastUpdatedDate',publishedDate);
        mapEmp.put('itemId',itemId);
        mapEmp.put('image',att.body);        
        String JSONString = JSON.serialize(mapEmp);       
        req.setBody(JSONString);         
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('Body ' + res.getBody());
        system.debug('----res'+res);
        if(res.getStatusCode() == 200)
        { 
            system.debug('Authentication success!!!' + res);           
             
        }
        else
        {
            system.debug('Authentication failed!!!' + res + res.getStatusCode());
          
        } 
        return null;
    }
Ginto MathewGinto Mathew
Hi Siju,
Just wanted to know what was the workaround for this. Even we have a similar requirement, but will have to send the image directly from StaticResources. The result body is returned as a blob, and the callout fails due to this.

Thanks!