David T.
David T.

Reputation: 23371

curl within Ruby stripping out paramters

For some reason, whenever i try to invoke a bash curl command within my ruby console, it always strips out all except the first parameter. can anyone please explain why only the first parameter is sent?

for example, if i have something like

`curl -F file=@../cheddar.txt -X POST http://myapp.com/endpoint?key=cheese&action=eat&when=now&id=#{some_id}&version=#{my_version}`

only the key parameter is accepted on the backend. However, the when or version parameter are all null. On my backend, im using Java google appengine, and it's a very standard servlet that has been tested and works with regular curl command. I know that the first parameter is sent because if i dont send the key, then something else will happen on backend (and likewise if i swap something else to the first parameter, that gets sent)

Any ideas what might be happening?

Upvotes: 0

Views: 87

Answers (1)

Keith Bennett
Keith Bennett

Reputation: 4970

I think your shell is reading the ampersands as shell commands. You need to escape strings you send to your shell so that it knows these characters are to be taken literally. Check out this script:

#!/usr/bin/env ruby

require 'shellwords'

url = 'http://myapp.com/endpoint?key=cheese&action=eat'

puts "With unescaped string:"
puts `echo #{url}`  # => "http://myapp.com/endpoint?key=cheese"
puts 'Note the absence of the last parameter action=eat'
puts "\nNow, with escaped string:"
escaped_url = Shellwords.shellescape(url)
puts `echo #{escaped_url}` # => "http://myapp.com/endpoint?key=cheese&action=eat"

Here's what Shellwords.shellescape does:

2.3.0 :014 > Shellwords.shellescape('http://myapp.com/endpoint?key=cheese&action=eat')
 => "http://myapp.com/endpoint\\?key\\=cheese\\&action\\=eat"

Another way to do this is to insert double quotes where appropriate, such as:

command = %q{echo "http://myapp.com/endpoint?key=cheese&action=eat"}
puts `#{command}`

Note that this behavior has nothing to do with curl; curl is just processing whatever it gets from the shell. So you would need to do this with other shell commands as well.

Upvotes: 1

Related Questions