You need to sign in to do that
Don't have an account?
Retrieving Attachments via REST API
Hi everyone,
I'm doing a rest call to retrieve attachments out of Salesforce. The first call would get the information of the attachment which has the URL to the body of the attachment. From there, I take the URL of the body and do another call to retrieve the contents of the attachment.
The calls are successful, however, when getting a response back, the response.getBody() return a string that does not translate to anything. When saving to a file or reuploading or encoding it, it always ends up to be gibberish or a broken file.
My questions are:
what kind of content does the GET method return for the URL: https://SOME_ORG.salesforce.com/services/data/v20.0/sobjects/Attachment/SOME_ID/body
Am I missing anything to get the correct format? The headers that are being included in the final call for my attachment body are Content-Type, Authoriatization, and Accept
I also forgot to mention that postman is able to retrieve and display the content just fine. The issue is that the rest call returns a binary string and I am unable to translate it back to the actual content itself.
EncodingUtil.convertFromHex(inputString);
EncodingUtil.base64Decode(inputString)
Hi SRK,
The rest calls are being made from a java class. I've been using Base64 encode and decode to make the raw text work. Any ideas on how I can use the EncodingUtil class from the rest call?
just to undersatnd it
1) you have a REST API in salesforce which query the attachement and send it back as a responce
2) now you have JAVA code which is calling this salesforce based REST API ?
if that is the case
in salesforce rest API side before sending responce back you can just convert the blob into text using .
EncodingUtil.base64Encode(File.body);
and then send it as text to java
JAVA have utality calss
Base64 b64 = new Base64();
Base64.decodeBase64(String that you are getting from salesforce)
It's something more like:
1) I have JAVA code doing a GET via REST which retrieves a response from Salesforce
2) A response is retrieved with a success code of 200 and a body with binary content
3) Base64.encode is used to encode the binary object
The issue I am having is that:
a) the original binary content itself does not create the image when saving it to a file
b) decoding the object itself gives me an error (probably because it's not encoded to begin with)
c) encoding the object is fine but when I decode it, the decoded object is unable to translate itself into anything (i.e a broken file)
I am not using apex code or anything from the Salesforce side. It's me querying against Salesforce via REST API with the use of JAVA.
Bitmap image = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(image);
Or try this
import java.io.ByteArrayOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
ByteArrayInputStream bis = new ByteArrayInputStream(your binarydata);
BufferedImage bImage2 = ImageIO.read(bis);
ImageIO.write(bImage2, "jpg", new File("output.jpg") );
Hi Vinh,
you want to give a try to Uint8Array
var fileContents = new Uint8Array(theResult);// Use the Uint8Array format to get the file contents
Hi SRK,
Thank you for your reply. Unfortunately, I am doing this with Java and not Javascript. I did try to find a Java equivalent to your solution, but that was unsuccessful.