Note that there are some explanatory texts on larger screens.

plurals
  1. POSending files to a Rails JSON API
    primarykey
    data
    text
    <p>I know there are questions similar to this one, but I've not found a good answer yet. What I need to do is send a description of an object to one of my create methods, which includes some different attributes including one called :image, a paperclip attachment:</p> <pre><code>has_attached_file :image </code></pre> <p>Now I've read that sending the image could be done straight in JSON by encoding and decoding the image as base64, but that feels like a dirty solution to me. There must be better ways.</p> <p>Another solution is sending a multipart/form-data request, much like the one <a href="http://leejava.wordpress.com/2009/07/30/upload-file-from-rest-in-ruy-on-rail-with-json-format/" rel="noreferrer">LEEjava describes here.</a> The problem with that one is that the request params are not interpreted correctly in Rails 3.2.2, and JSON.parse spits out an error when it tries to parse the params, or perhaps it is Rails that is misinterpreting something.</p> <blockquote> <p>Started POST "/api/v1/somemodel.json?token=ZoipX7yhcGfrWauoGyog" for 127.0.0.1 at 2012-03-18 15:53:30 +0200 Processing by Api::V1::SomeController#create as JSON Parameters: {"{\n<br> \"parentmodel\": {\n \"superparent_id\": 1,\n<br> \"description\": \"Enjoy the flower\",\n \"\": "=>{"\n<br> {\n \"someattribute\": 1,\n<br> \"someotherattribute\": 2,\n \"image\": \"image1\"\n<br> }\n "=>{"\n }\n}"=>nil}}, "token"=>"ZoipX7yhcGfrWauoGyog"}</p> </blockquote> <p>It is quite hard to read that, sorry. JSON.parse(params[:parentmodel]) is not possible here, and I can't JSON.parse(params) either because of the token attribute, JSON.parse(params) throws this error:</p> <blockquote> <p>TypeError (can't convert ActiveSupport::HashWithIndifferentAccess into String)</p> </blockquote> <p>Which leads me to believe I'm either approaching this problem totally wrong, or I'm just doing something. Either way, we can be sure that I'm wrong about something. :)</p> <p>Is there a better way to do this? Can someone point me to any guide/tutorial, or write an answer describing how I should approach this?</p> <p>Thank you in advance</p> <p><strong>UPDATE:</strong> So I've actually got it working now, but only in tests. I'm not totally sure how this works, but perhaps someone can fill in the gaps for me? This is part of the test code (the image: fixture_file_upload(...) is the important part).</p> <pre><code>parts_of_the_object = { someattribute: 0, someotherattribute: 0, image: fixture_file_upload('/images/plot.jpg', 'image/jpg') } </code></pre> <p>My params[] looks like a normal HTML form was submitted, which is strange (and awesome):</p> <pre><code>Parameters: {"superparentid"=&gt;"1", "plots"=&gt;[{"someattribute"=&gt;"0", "someotherattribute"=&gt;"0", "image"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x007f812eab00e8 @original_filename="plot.jpg", @content_type="image/jpg", @headers="Content-Disposition: form-data; name=\"plots[][image]\"; filename=\"plot.jpg\"\r\nContent-Type: image/jpg\r\nContent-Length: 51818\r\n", @tempfile=#&lt;File:/var/folders/45/rcdbb3p50bl2rgjzqp3f0grw0000gn/T/RackMultipart20120318-1242-1cn036o&gt;&gt;}], "token"=&gt;"4L5LszuXQMY6rExfifio"} </code></pre> <p>The request is made just like and post request is made with rspec:</p> <pre><code>post "/api/v1/mycontroller.json?token=#{@token}", thefull_object </code></pre> <p>So I've got it all working. I just don't know how exactly it works! I want to be able to create a response like this by myself too, not only from RSpec. :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload