This is a demonstration of making a cPanel XMP API call via a ruby script.
For this you will require:
for curl-devel ( development stuff ,if you dont have it), do
$ yum install curl-develThen we install ‘curb’.The ‘curl’ module for ruby.
$ gem install curbHere 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: automation, cPanel, curb, curl, gem, hosting, ruby, xml-api










Hi,
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
Thanks for the snippet!
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?
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_bodyhandler to print the response).@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
Awesome! Thanks for including ruby code to have multiple parts for header. I had the most challenging time finding an example to follow!