John Powel
John Powel

Reputation: 1424

How can I use curl to upload a file using paperclip?

I want to upload a file together with some information(e.g. package_type) with curl

in my submission model:

has_attached_file :package

What I tried:

curl -d "submission[package_type]=type1&submission[package]=@/home/ubuntu/Downloads/test.zip" http://localhost:3000/restapi.json

If I leave out the file object, it works(a entry will be inserted into the database)

But I specify the file like above, it gives me an error:

No handler found for "@/home/ubuntu/Downloads/test.zip"

Update:

I just found that that I should use the -F option in curl, but in that case the file information cannot be recorded, is there anyway to include both the file object and file info? Maybe something like curl -d -F ?

Upvotes: 2

Views: 720

Answers (1)

Tom Fast
Tom Fast

Reputation: 1158

I had a similar issue and ended up setting the content-type to multipart/form-data instead of dealing with base64 encoding issues when posting to my REST API. Here is an example which includes headers for auth:

curl -v -H 'Content-Type: multipart/form-data' -H "X-User-Email: <email>" -H "X-User-Token: <token>" -X POST -i -F submission[package_type]=type1 -F submission[image_attributes][image][email protected] http://localhost:3000/api/v1/submissions

Upvotes: 2

Related Questions