AnsweredAssumed Answered

File attachments with REST API and JAVA

Question asked by Brayan Bustillos on Dec 19, 2017
Latest reply on Dec 21, 2017 by Ken McCartney

Hi,

 

I was trying to upload file attachments based on all examples I found in the web... but unfortunately I couldn't achieve my objective.

First one I created a new "Note" with the following body:

{"parent":{"id":"092e194e-e4c5-11e7-89b6-061669249537"},"filename":"doc20.pdf","name":"document20","description":"document desc: 20"}

making a relation with an opportunity "parent":{"id":"092e194e-e4c5-11e7-89b6-061669249537"} -> (opportunityId)

I retrieve the noteId created and I put it over the endpoint as follows:

endpointURL= host + "/rest/v10/Notes/" + noteId+"/file/filename";

After of this I tried to upload the attachment with the following code:

The jsonBody is generated in the following context:

JSONObject bodyPDF = new JSONObject();
if (filePath == null){
    filePath = "/pathToFile/Document1.pdf";
}
bodyPDF.put("format", "sugar-html-json");
bodyPDF.put("delete_if_fails", "false");
if (!StringUtils.isEmpty(accessToken))
    bodyPDF.put("oauth-token", accessToken);
bodyPDF.put("filename", filePath);
jsonBody =  bodyPDF.toJSONString();
URL url = URIBuilder(path).toURL();
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = jsonBody.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
// first time doesn't access
if (!StringUtils.isEmpty(accessToken))
    httpConn.setRequestProperty("oauth-token", accessToken);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();

 

But always I get the same response:

 

{"error":"missing_parameter","error_message":"Attachment is missing"}

 

I don't know if somebody can help me...

Outcomes