3

I am trying to send a binary file in Java to Salesforce '/sobject/Attachment' endpoint. Below is the code I am trying to use, but I keep receiving a 400 error. Any idea as to what the solution is?

public void postAttachmentToSalesforce(String campaignId, File upload) throws HttpException, IOException {

    // convert File to byte array
    byte[] byteFile = new byte[(int) upload.length()];
    FileInputStream fs = new FileInputStream(upload);
    fs.read(byteFile);
    fs.close();

    // encode byte[] as Base64 to send to SF
    byte[] encodedFile = Base64.getEncoder().encode(byteFile);

    JSONObject toPost = new JSONObject();
    toPost.put("Body", encodedFile);
    toPost.put("ParentId", campaignId);
    toPost.put("Name", upload.getName());   

    // open new multipart
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(authToken.getInstanceUrl() + versionUrl + "/sobjects/Attachment/");
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    post.addHeader("Authorization", "OAuth " + authToken.getAccessToken());
    post.addHeader("X-PrettyPrint", "1");
    post.addHeader("Content-Type", "multipart/form-data");

    builder.addBinaryBody("Body", encodedFile);

    builder.addTextBody("ParentId", campaignId);
    builder.addTextBody("Name", upload.getName());

    HttpEntity multi = builder.build();
    post.setEntity(multi);

    CloseableHttpResponse response = httpClient.execute(post);
    System.out.println(response);
}

Here is the response:

HttpResponseProxy{HTTP/1.1 400 Bad Request [Date: Fri, 23 Sep 2016 19:46:25 GMT, Content-Type: text/html;charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: text/html;charset=UTF-8,Chunked: true]}}
1

Update - attempt #2

I don't have a java environment to test with but I changed your provided code based on some other code I've got handy and I've got my fingers crossed.

I changed out the MultipartEntityBuilder for a StringEntity and then serialized the JSONObject which you already had but were not using. I also changed the method on the encoder to output a String to be included in the JSON body instead of the byte array.

public void postAttachmentToSalesforce(String campaignId, File upload) throws HttpException, IOException {

    // convert File to byte array
    byte[] byteFile = new byte[(int) upload.length()];
    FileInputStream fs = new FileInputStream(upload);
    fs.read(byteFile);
    fs.close();

    // encode byte[] as Base64 to send to SF
    String encodedFileAsString = Base64.getEncoder().encodeAsString(byteFile);

    JSONObject toPost = new JSONObject();
    toPost.put("Body", encodedFileAsString);
    toPost.put("ParentId", campaignId);
    toPost.put("Name", upload.getName());   

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(authToken.getInstanceUrl() + versionUrl + "/sobjects/Attachment/");
    post.addHeader("Authorization", "Bearer " + authToken.getAccessToken());
    post.addHeader("Content-Type", "application/json");

    StringEntity params = new StringEntity(toPost.toString(), "UTF-8");
    params.setContentType("application/json");
    post.setEntity(params);

    CloseableHttpResponse response = httpClient.execute(post);
    System.out.println(response);
}

Original response

On first look, I would suspect that it is your Authorization header value being OAuth concatenated with the token instead of the usual Bearer and then the token.

Does this give you a different result?

 post.addHeader("Authorization", "Bearer " + authToken.getAccessToken());

SFDC Docs: REST API Insert or Update Blob data

  • Switching it to Bearer still returns a 400 – xtheking Sep 23 '16 at 19:49
  • @xtheking updated the answer with another possible solution. – Mark Pond Sep 23 '16 at 21:14
  • Works! Marked it as the correct answer. Thank you so much. – xtheking Sep 26 '16 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.