Simple cPanel XML api call using Ruby

By in articles, cPanel, ruby, Server Admin, Tutorials, Web on April 2, 2010

This is a demonstration of making a cPanel XMP API call via a ruby script.
For this you will require:

  • #A working Ruby installation (1.8+, tested with 1.8.5)
  • #A working (lib)curl installation, with development stuff (7.5+)

  • for curl-devel ( development stuff ,if you dont have it), do

    $ yum install curl-devel

    Then we install ‘curb’.The ‘curl’ module for ruby.

    $ gem install curb

    Here is the simple demonstration of the API call using Basic Authentication:
    File : cp_ruby.rb

    require 'rubygems' #to load the curl module
    require 'curl' #for the Curl class
    require 'base64' #for the authentication
     
    url="https://example.com:2087/xml-api/version"
    user="root"
    pass="somePass"
     
    str=user+":"+pass
    authStr=Base64.encode64(str)
     
    crl = Curl::Easy.new(url)
    crl.headers={"Authorization"=>"Basic "+authStr+"\r\n",
                    "Content-Type"=>"application/x-www-form-urlencoded\r\n"}
     
     
    crl.ssl_verify_peer= FALSE
    crl.ssl_verify_host= FALSE
     
    crl.perform
    puts crl.body_str

    Thats it.
    Now,

    $ ruby cp_ruby.rb

    You’ll get the following response:

    <version>
      <version>11.25.0</version>
    </version>

    Tags: , , , , , , ,

    4 Responses to “Simple cPanel XML api call using Ruby”

    1. Prachi says:

      Hi,
      Thanks for the snippet! :) Works fine.. But there’s a little something that has been bothering me. I am using curb to access a page that requires HTTP authentication. The docs mention ‘username=’ and ‘password=’ methods to set the HTTP authentication username and password. But something like


      easy = Curl::Easy.new('someurl')
      easy.username = 'username'
      easy.password = 'password'
      easy.perform

      does not work. I even set the http_auth_types with


      easy.http_auth_types = Curl::CURLAUTH_BASIC

      I might be doing something silly here. Could you hep me out?

    2. Prachi says:

      Sorry for posting again, but I missed a little information in the previous comment. I get a “401 not authorized” error with that code snippet (I used the on_body handler to print the response).

    3. broncha says:

      @Prachi: If you see the snippet above I have used the HTTP authentication method.In your case, it would be:

      easy.headers={"Authorization"=>"Basic "+authStr+"\r\n",
      "Content-Type"=>"application/x-www-form-urlencoded\r\n"}

      The authStr is base64 encoded string with colon separated username and password.
      Try with this code:

      authStr = Base64.encode64("username:password")
      easy = Curl::Easy.new('someurl')
      easy.headers={"Authorization"=>"Basic "+authStr+"\r\n",
      "Content-Type"=>"application/x-www-form-urlencoded\r\n"}
      easy.perform

    4. Enrique says:

      Awesome! Thanks for including ruby code to have multiple parts for header. I had the most challenging time finding an example to follow!

    Leave a Reply