0

I am trying to send a file to a controller action via curl. Here is what I tried:

curl -F "file=wb.csv" http://localhost:3000/api/v1/process_data?api_key=Dp9Kv7j1y-FYytd-tYsAsSNic3ox

However, in my controller, I checked the content of the params sent and it just returns the filename wb.csv instead of a csv attachment:

{"file"=>"wb.csv", "api_key"=>"Dp9Kv7j1y-FYytd-tYsAsSNic3ox", "format"=>"json", "action"=>"import_csv", "controller"=>"api/v1/growth_utils"}

I'd like to achieve the same thing as if I was posting from a form on the UI:

<%= form_tag import_csv_admin_growths_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<%end%>

Which in the controller action has params:

{"file"=>
      #<ActionDispatch::Http::UploadedFile:0x007faa0543b7d0
       @content_type="text/csv",
       @headers="Content-Disposition: form-data; name=\"file\"; filename=\"wb.csv\"\r\nContent-Type: text/csv\r\n",
       @original_filename="wb.csv",
       @tempfile=#<File:/var/folders/zq/kk1mrjjn52zdf8120c4z4tb80000gn/T/RackMultipart20180405-21787-17k4lup>>,
     "commit"=>"Import",
     "format"=>"csv",
     "action"=>"import_csv",
     "controller"=>"admin/growths"}

1 Answer 1

2

In your curl you are doing it wrong. You are sending the file name as parameter not the file itself

curl -F [email protected] http://localhost:3000/api/v1/process_data?api_key=Dp9Kv7j1y-FYytd-tYsAsSNic3ox.

try this. this is same as your second output and that's how rails handle file uploads.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked. thanks. The issue was quotes around [email protected] ?\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.