You need to sign in to do that
Don't have an account?
Esha Sharma 3
Use Rest API to upload attachments
Hi Guys,
I am trying to hit below URL to use Attachments API and I am getting BAD Request. Can you help me with the right URL and point out if anything is missed.
builder = new PostMethod("https://gus,salesforce.com/" + "/services/data/v20.0/sobjects/Attachment/");
builder.setRequestHeader("Authorization","OAuth " + lr.getSessionId());
httpclient.executeMethod(builder);
System.out.println(builder.getStatusText()); << throws Bad Request
I am trying to hit below URL to use Attachments API and I am getting BAD Request. Can you help me with the right URL and point out if anything is missed.
builder = new PostMethod("https://gus,salesforce.com/" + "/services/data/v20.0/sobjects/Attachment/");
builder.setRequestHeader("Authorization","OAuth " + lr.getSessionId());
httpclient.executeMethod(builder);
System.out.println(builder.getStatusText()); << throws Bad Request
This request is really urgent. Want somebody to really point out what is missing here?
I guess your endpoint is incorrect:
Please take a look this link:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm
Go to section: Session ID Authorization
After login, salesforce will response Session ID & Instance, so you need to replace instance_name
Hope it works.
builder = new PostMethod("https://gus.salesforce.com/services/data/v20.0/");
builder.setRequestHeader("Authorization","Bearer " + lr.getSessionId());
httpclient.executeMethod(builder);
System.out.println(builder.getStatusText()); << Method not allowed
Some guideline:
- Convert your file to "Binary data"
- There's another http header Content-Type
- Construct newdocument.json as follow
Click here (https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm)for more complete steps
I would suggest you try simple functionality first, just to get familiar with REST API. You can try to create a record using rest api. Please follow this step by step: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm
What programming language do you use?
Another suggestion, you can use curl to test out REST API.
I am using Java for this. Can you also point out any working example for uploading file as "Binary data" using Rest.
I don't have any working example. But here is the overview how to upload file using rest:
1. Login to salesforce, get session id & instance url
2. Prepare binary data and construct request body
Here is a link (http://blog.icodejava.com/248/how-to-base64-encode-or-base64-decode-a-file-in-java-using-apache-commons-codec/) to convert file to binary data. The binary data (base64 encoded) will be used in code below. Change description, keywords, folder id (mandatory), name and type
Change content-type & content-disposition as well
3. Set header, endpoint & session id
4. Execute callout rest api.
Salesforce will response either success or fail
Success
Fail
Below is what I am doing:
byte[] data = IOUtils.toByteArray(new FileInputStream(f));
JSONObject content = new JSONObject();
if (attachment != null) {
content.put("Name", attachment);
}
content.put("Body", new String(Base64.encode(data)));
content.put("ParentId", caseid);
builder = new PostMethod("https://gus.salesforce.com/services/data/v20.0/sobjects/Attachment/");
builder.setRequestEntity(new StringRequestEntity(content.toString(), "application/json", null));
builder.setRequestHeader("Authorization","OAuth " + SessionId);
httpclient.executeMethod(builder);
This code works for me but for files for smaller size. I tried attaching file of 50+ MB and it threw bad request error for me. Can you please help. I need a solution for attaching files of larger size (around 1GB).
Regards,
-Esha
If anyone is still looking for a solution, this is my code in PHP which uploads the attachment to Salesforce Documents.
This is my blog post with complete explanation: https://initaura.com/uploading-attachment-through-rest-api-salesforce-simple-working-solution/
Thanks and Regards,
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://www.initaura.com)
I saw this post and the documentation of this API and I think that I'll need to use it.
I need to call the Document API but I don't understand the relationship between Document and ContentDocument. How can a relate both?
The requirement is I need to upload a file and associate it to a record. I already have the id of my record. How can I do this through API calls?
Regards,
Gabriel Uchôa