0

I need to convert this multi-part form that contains some json and also uploads a file.

curl \
-F metadata='{"userID":"12345","mimeType":"image/jpeg","typeHint": "uploadedImage" }' \
-F contents=@$HOME/Downloads/photo.jpg \
http://localhost:8083/myservice/upload
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191

1 Answers1

1

With curl

As is

The obvious answer would be to just call the given command from Ruby :

curl_command = %q(curl -F metadata='{"userID":"12345","mimeType":"image/jpeg","typeHint": "uploadedImage" }' -F contents=@$HOME/Downloads/photo.jpg http://localhost:8083/myservice/upload)

system(curl_command)

%q() is used to define a string since you have many characters that would need to be unescaped otherwise (e.g. " or ')

with parameters

If you want dynamic parameters, you could use :

def curl_command(user_id, file, url)
  %Q(curl -F metadata='{"userID":"#{user_id}","mimeType":"image/jpeg","typeHint": "uploadedImage" }' -F contents=#{file} #{url})
end

picture = File.join(Dir.home, 'Downloads', 'photo.jpg')
puts curl_command(12345, picture, "http://localhost:8083/myservice/upload")
#=> curl -F metadata='{"userID":"12345","mimeType":"image/jpeg","typeHint": "uploadedImage" }' -F contents=/home/eric/Downloads/photo.jpg http://localhost:8083/myservice/upload

system(curl_command(12345, picture, "http://localhost:8083/myservice/upload"))

With curb

It looks like the curb gem can do what you want :

HTTP POST file upload:

c = Curl::Easy.new("http://my.rails.box/files/upload")
c.multipart_form_post = true
c.http_post(Curl::PostField.file('thing[file]', 'myfile.rb'))
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 1
    It would work fine, but IMO doesn't really answer the question, at least as I read it, it's the same as answering "How to I print something in ruby" with `system("echo #{my_text}")`. It's technically correct, but it's not really useful. – thesecretmaster Jan 26 '17 at 10:40
  • It *does* answer the question in the form it was asked: `I have a curl command that I need to convert to ruby` - it *is* converted to Ruby. +1 – Andrey Deineko Jan 26 '17 at 10:44
  • While I don't know about the down vote, but I do agree with @thesecretmaster. The question basically asks someone: "Can you do my work?", and it really annoys me to see that some people just do. – lcguida Jan 26 '17 at 10:55
  • @lcguida : I just wanted to leave a short and lazy answer, but that wasn't enough for thesecretmaster. I added some more information, without completely answering the question. I just looked for documentation and pasted it, without adapting it to the OP needs. – Eric Duminil Jan 26 '17 at 10:57
  • @Icguida The downvote alt-text says "This answer is not useful." I would argue that just using `system` is not useful. Now that the curb stuff is there, it's a good answer (except it's already answered both ways in the dupe.) Also, you don't say how to do dynamic params in curb, but at least now it's not a DV worthy answer. I would say the curb stuff should be a separate answer, but that's just my opinion. – thesecretmaster Jan 26 '17 at 10:59
  • I do see your point. Mine is a question like this shouldn't be even answered. Someone who comes here and asks: "I need to do this? How do I do it?" without showing the minimum effort to even begin something, is clearly lazy enough to just wait for an answer. – lcguida Jan 26 '17 at 11:00
  • Thanks for that. How would you add the json in that curb example? – code_monkey Jan 26 '17 at 11:01
  • @code_monkey Have you looked at the question this is marked as a duplicate of? There are a few different ways listed there. – thesecretmaster Jan 26 '17 at 11:02
  • Looking now, apologies if I've annoyed some of you. – code_monkey Jan 26 '17 at 11:05
  • 1
    I don't think that orthogonal answers are necessarily bad. I've been helped many times by SO answers that interpret the question differently than most people did. Also, using `system` is sometimes just the thing: Its runtime performance isn't too bad in many cases, and if you already having a working curl command, it's faster to code than converting the command to native Ruby. – Wayne Conrad Jan 26 '17 at 11:10