Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP-POST an image to Ruby on Rails app

I have an image in the form of a byte array in java. I'm trying to upload that image to my Ruby on Rails app, which makes use of Paperclip gem. My Rails model looks like this:

class App < ActiveRecord::Base
  has_attached_file :image
end

When I execute the java code, I get HHTTP/1.1 302 Found response my rails app. Updated java HTTP-Post method. Here's the new java code that I am using for HTTP-Post:

public void postFile(byte[] image) throws ClientProtocolException, IOException{
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httppost = new HttpPost("http://localhost:3000/apps");

    ContentBody cb = new ByteArrayBody(image, "image/jpg", "icon.jpg");
    //ContentBody cb = new InputStreamBody(new ByteArrayInputStream(image), "image/jpg", "icon.jpg");

    MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    mpentity.addPart("image", cb);
    httppost.setEntity(mpentity);

    HttpResponse response = httpclient.execute(httppost);
    System.out.println(response.getStatusLine());
}

Here's the new debug on the rails console:

Started POST "/apps" for 127.0.0.1 at Mon Jan 31 00:26:07 +0000 2011
Processing by AppsController#create as HTML
Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x1041491c8 @content_type=nil, @original_filename="icon.jpg", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"icon.jpg\"\r\n", @tempfile=#<File:/var/folders/NU/NUcfnxwVHmmnVUo9JUdNok+++TI/-Tmp-/RackMultipart20110131-3735-1ylugl7-0>>}
SQL (0.1ms)  BEGIN
SQL (1.0ms)  describe `apps`
AREL (0.4ms)  INSERT INTO `apps` (`appName`, `image_file_size`, `created_at`, `image_updated_at`, `image_file_name`, `image_content_type`, `updated_at`) VALUES (NULL, NULL, '2011-01-31 00:26:07', NULL, NULL, NULL, '2011-01-31 00:26:07')
[paperclip] Saving attachments.
SQL (1.3ms)  COMMIT
Redirected to http://localhost:3000/apps/36
Completed 302 Found in 18ms

Uploading images directly from the browser works like a charm. Here's the implementation of controller/apps_controller.rb:

def create
  @app = App.new(params[:app])

  respond_to do |format|
    if @app.save
      format.html { redirect_to(@app, :notice => 'App was successfully created.') }
      format.xml  { render :xml => @app, :status => :created, :location => @app }
      format.json  { render :json => @app, :status => :created, :location => @app }        
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @app.errors, :status => :unprocessable_entity }
      format.json  { render :json => @app.errors, :status => :unprocessable_entity }        
    end
  end
end

Where am I going wrong?

like image 557
Raunak Avatar asked Jan 30 '11 20:01

Raunak


1 Answers

I think the problem is that you try to create the App with params[:app] when there are no parameters named app, only image. I think the following in your create action should work instead:

@app = App.new(:image => params[:image])

Another good thing can be to add the following validation in your App model to make sure that a record is not saved to the database if no image was saved.

validates_attachment_presence :image
like image 99
DanneManne Avatar answered Oct 24 '22 19:10

DanneManne