2

The following command (and variations of it) run okay in terminal in mac, run okay in a ruby program executed on a mac, run okay directly in the windows command prompt, but fail with a parsing error when I try to run it inside of a ruby file on windows.

curl -u"user:pwd" -d"{\"name\":\"new_repo_beepo\"}" https://api.github.com/user/repos --insecure

I've tried executing it with backticks, %x() and system. I've also tried substitution of strings and json'ing pieces of it, without any luck. From what I can determine, the failure point is in the -d"{\"name\":\"repo_name\"}" section, but that's only from trying the command without it. Regardless, in each variation of the command on windows in ruby, I get a JSON parsing error.

5
  • 3
    Possibly a parsing/quoting problem with the shell mechanism used by ruby to run the command on windows. Have you tried the tokenized form of system, eg: system('curl','-u"user:pwd"', '-d...', ...)? On posix that will send the arg vector as specified directly to the exec'ed program without letting the shell get in the way. Commented May 17, 2012 at 18:37
  • Why curl anyway? In ruby you can use Net::HTTP to do this directly. Commented May 17, 2012 at 18:39
  • Thanks, dbenhur -- I will give the tokenized form a shot this afternoon. As for your other question, our systems run jruby and the native extensions seem to be a problem for Net:HTTP, curb, and the others I found. Commented May 17, 2012 at 19:13
  • @dbenhur: The tokenized version worked perfectly! Thanks a lot! Commented May 17, 2012 at 19:30
  • @dbenhur: by the way, if you want to submit that same suggestion as an answer I will choose it. Commented May 17, 2012 at 19:31

2 Answers 2

2

Have you tried using rest-client?

It is a gem and works quite nice. It is probably better than using system()or %x() and it is definitely more secure (You can inject malicious bash commands on system() so it needs to be used carefully).

It is quite simple to use. Just install the gem and require it one your ruby file.

NOTE: If using Rails just add it to your Gemfile(No need to require it on each file).

require 'rest_client'

RestClient.get 'https://api.github.com/user/repos', {params: {id: 50, foo: 'bar'}}

You can also use some params for --insecure ssl.

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

Comments

1

It seems likely this is a parsing/quoting problem with the shell mechanism used by ruby to run the command on windows. Have you tried the tokenized form of system, eg:

system('curl', '-u"user:pwd"', '-d"{"name":"new_repo_beepo"}"', 'https://api.github.com/user/repos', '--insecure')

On posix that will send the arg vector as specified directly to the exec'ed program without letting the shell get in the way; probably the same semantics hold for windows.

You shouldn't need to exec curl to do this as ruby has stdlib Net::HTTP. In comments you mentioned that you've had problems with this module under jruby, but we have jruby services here exercising http[s] requests just fine, so you might try posting a question addressing the specific problems you have with jruby and native http client libs.

Comments

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.