title: Cannot Upload Image or attachment to Salesforce chatter Rest API
body: <p>I am integrating Salesforce in my application.I am required to show <strong>Chatter feed in my application</strong> and in the process,I have done all functionalities of Chatter Rest Api(Posting,Fetching data,Comment,Likes etc),but <strong>I am not able to upload any kind of attachment to the chatter API</strong>.</p>
  
  <p>I have consulted the following links:</p>
  
  <p><a href="https://github.com/gauravkheterpal/boxForce/blob/master/src/com/metacube/boxforce/SalesForceObjectChooser.java" rel="nofollow noreferrer">https://github.com/gauravkheterpal/boxForce/blob/master/src/com/metacube/boxforce/SalesForceObjectChooser.java</a></p>
  
  <p><a href="https://github.com/noeticpenguin/CasePhotographer/blob/master/src/com/brightleafsoftware/casephotographer/CaseDetailFragment.java" rel="nofollow noreferrer">https://github.com/noeticpenguin/CasePhotographer/blob/master/src/com/brightleafsoftware/casephotographer/CaseDetailFragment.java</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36103724/how-do-i-upload-file-using-salesforce-mobile-sdk-for-android">How do I upload file using Salesforce Mobile SDK for android?</a>​</p>
  
  <p>My code:</p>
  
  <pre><code> private void postImageAsAttachment(String parentId, String title,String filepath) {
          Map&lt;String, Object&gt; fields = new HashMap&lt;String, Object&gt;();
          fields.put("Name", title);
          fields.put("ParentID", "The user id");
          File file=new File(filepath);
          fields.put("Body", encodeFileInBase64(file));
          fields.put("ContentType", "image/jpeg");
  
          RestRequest request = null;
          try {
              request = RestRequest.getRequestForCreate(getString(R.string.api_version), "Attachment", fields);
              System.out.println("Request File:"+request);
          } catch (Exception ex) {
              Log.d(TAG, "sendRequest: ", ex);
              Toast.makeText(MyNewsFeed.this, "The file upload failed: " + ex.toString(), Toast.LENGTH_LONG).show();
          }
          client.sendAsync(request, new RestClient.AsyncRequestCallback() {
  
              @Override
              public void onSuccess(RestRequest request, RestResponse result) {
  
                  try {
                      //Do something with JSON result.
                      // System.out.println("Restresponse---&gt;"+result);
                      println(result);  //Use our helper function, to print our JSON response.
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  EventsObservable.get().notifyEvent(EventsObservable.EventType.RenditionComplete);
              }
  
              @Override
              public void onError(Exception e) {
                  e.printStackTrace();
                  EventsObservable.get().notifyEvent(EventsObservable.EventType.RenditionComplete);
              }
          });
      } 
  </code></pre>
  
  <p>I am calling this method from <code>onActivityResult</code> <strong>after fetching the filepath</strong> which I want to upload in the Chatter.</p>
  
  <p>The following method <strong>encodes the image to Base64</strong>:</p>
  
  <pre><code> private String encodeFileInBase64(File file) {
          String fileTxt="";
          StringBuilder sb = new StringBuilder();
          // StringBuilder sbStr = new StringBuilder((int) (file.length() / 3 *
          // 8));
  
          FileInputStream fin = null;
          try {
              fin = new FileInputStream(file);
  
              int bSize = 3000; // 3 * 512;
              Log.v("fileLength", String.valueOf(file.length()));
  
              byte[] buf = new byte[bSize];
  
              int len = 0;
  
              while ((len = fin.read(buf)) != -1) {
                  Log.v("len", String.valueOf(len));// sbStr.append(new
                  // String(buf, 0, len));
  
                  byte[] encoded = Base64.encode(buf, Base64.DEFAULT);
  
                  sb.append(new String(encoded, 0, len));
              }
  
              Log.v("txtFileWithBuffer", sb.toString());
  
              fin.close();
          } catch (IOException e) {
              if (null != fin) {
                  try {
                      fin.close();
                  } catch (IOException e1) {
                      e1.printStackTrace();
                  }
              }
  
  
              String base64EncodedFile = sb.toString();
  
  
              FileInputStream fileInputStream = null;
  
              byte[] bFile = new byte[(int) base64EncodedFile.length()];
  
              try {
                  fileInputStream = new FileInputStream(base64EncodedFile);
                  fileInputStream.read(bFile);
                  fileInputStream.close();
                  fileTxt = Base64.encodeToString(bFile, Base64.DEFAULT);
                  Log.v("txtFileWithoutBuffer", fileTxt);
  
                  return fileTxt;
  
                  // encodeImgList.add(encodedImage);
  
              } catch (Exception ex) {
  
                  ex.printStackTrace();
  
                  return "";
              }
  
              // return base64EncodedFile;
  
          }
          return fileTxt;
      }
  </code></pre>
  
  <p>Whenever I execute the method,it gives me error:</p>
  
  <pre><code>BasicNetwork.performRequest: Unexpected response code 400 for https://ap2.salesforce.com/services/data/v36.0/sobjects/Attachment
  07-25 18:41:39.176 26386-26386/com.salesforce.th W/System.err: com.android.volley.ServerError
  07-25 18:41:39.176 26386-26386/com.salesforce.th W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:179)
  07-25 18:41:39.177 26386-26386/com.salesforce.th W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
  </code></pre>
  
  <p>I understand that this error only comes when there is a malformed XML or JSON body.But I didn't found what wrong I am doing here.</p>
  
  <p>I cannot understand what I am missing here.Please help.</p>