This is how I decided.
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for (NameValuePair nameValuePair : nameValuePairs) { if (nameValuePair.getName().equalsIgnoreCase("picture")) { File imgFile = new File(nameValuePair.getValue()); FileBody fileBody = new FileBody(imgFile, "image/jpeg"); multipartEntity.addPart("post[picture]", fileBody); } else { multipartEntity.addPart("post[" + nameValuePair.getName() + "]", new StringBody(nameValuePair.getValue())); } } httpPost.setEntity(multipartEntity); HttpResponse response = httpClient.execute(httpPost, httpContext);
This will result in a POST as follows:
{"post"=>{"description"=>"fhgg", "picture"=>
In the rails application, your model attributes should have the same name that you use in your request, so in my case
class Post < ActiveRecord::Base attr_accessible :description, :user_id, :picture has_attached_file :picture # Paperclip stuff ... end
I also disabled the CSRF token from the rails application.
source share