2

I'm using IO.popen to run a curl command (octet stream POST). I copy the command and run it on a command line and it works. Running it in Ruby with popen, however, causes the stream to end unexpectedly on my server and the Ruby script just hangs, with curl endlessly running.

Here's what I'm doing:

curl = "curl "
curl += "--cookie \"JSESSIONID=#{@sessionid}\" "
curl += "-H \"Content-type:application/octet-stream\" "
curl += "-X POST "
curl += "-o \"#{responseFile}\" "
curl += "--data-binary \"@#{filename}\" "
curl += "\"#{url}\""

puts "command: #{curl}"

IO.popen(curl, "r") { |out|
  out.readlines
}
# Script never makes it to here

I must be doing something silly. What is it?

Here is the stdout from curl.

I am using Ruby 1.8.6

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Tony R
  • 11,224
  • 23
  • 76
  • 101

2 Answers2

0

The popen also gives curl a pipe to read from, as this example shows:

$ echo -e 'foo\nbar\nzoo' | \
  ruby -e 'pipe = IO.popen("grep oo", "r") {|out| p out.readlines}'
["foo\n", "zoo\n"]

I guess that curl wants to POST to the server also from stdin, where nothing is coming from, so it comes to a 30s timeout.

Martin Vidner
  • 2,307
  • 16
  • 31
  • The man information for the -data parameter (to which --data-binary is similar) implies that using "@" before the parameter causes curl to post from the file instead of stdin. – Tony R Dec 22 '11 at 18:26
0

Rather than write your own version of a wheel, why not take advantage of the many gems for Ruby that do the same thing?

I'd recommend and typhoeus, curb for starting points. Also the built-in OpenURI works nicely for most "get" type needs.

There's a useful thread on SO for this post requests also.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Yea, don't I know it. Unfortunately my code runs in a Ruby environment that is missing the standard libraries... This is actually the entire reason I had to write this little nugget. I have used RestClient in the past with great success. – Tony R Dec 22 '11 at 18:20
  • BUT if you can either compile the standard C extensions on Mac for Ruby 1.8.5 (I tried and couldn't get them all) OR find me an HTTP library that doesn't rely on any C extensions or features >1.8.5, let me know =) – Tony R Dec 22 '11 at 18:31