Search code examples
ruby-on-railsrubycurldigestdigest-authentication

How to perform this kind of CURL request on Ruby


I am using an API provided by my web host with whom I can register new clients and domains. Unfortunately they don't provide much documentation (basically none) and I'm not very used to cURL

They give ONE and very superficial example of how to create a new client, and it is as follows

curl -d "clienteTipo=I&clienteCPFCNPJ=00112135045&clienteEmpresa=NomeEmpresa&clienteNome=meunome&[email protected]&[email protected]&clienteSenhaPainel=654321&clienteFone=555100000000&clienteFax=555100000001&clienteCEP=44587421&clienteEndereco=ruanome&clienteBairro=meubairro&clienteCidade=porto alegre&clienteEstado=rs&clienteLimiteMapeamento=1&clienteLimiteSubdominio=2&clienteLimiteMysql=3&clienteLimiteMssql=1&clienteLimitePgsql=1&clienteLimiteFirebird=1&clienteLimiteFTPADD=1&clienteUniBox=on&clienteAcessoFTP=on&clienteAcessoDownloadBackup=on" -k --digest -u usuario:senha -X POST https://api.kinghost.net/cliente

How can I transform this on a cURL request on Ruby keeping the parameters as informed above?

This must be a POST, but I wonder how I have to inform these parameters, they look like a query string. And what is this Digest thing?


Solution

  • after struggling for a while and testing different libraries, I used Ruby to run a system command, since cURL is not dependant on Ruby

    output = `curl -d "clienteTipo=#{client_type}&clienteCPFCNPJ=#{client_id}&clienteEmpresa=#{company_name}&clienteNome=#{client_name}&clienteEmail=#{client_email}&clienteEmailCobranca=#{client_billing_email}&clienteSenhaPainel=#{password}&clienteFone=#{client_phone}&clienteFax=#{client_fax}&clienteCEP=#{client_zip_code}&clienteEndereco=#{client_address}&clienteBairro=#{client_neighborhood}&clienteCidade=#{client_city}&clienteEstado=#{client_state}&clienteLimiteMapeamento=1&clienteLimiteSubdominio=1&clienteLimiteMysql=1&clienteLimiteMssql=1&clienteLimitePgsql=1&clienteLimiteFirebird=1&clienteLimiteFTPADD=1&clienteUniBox=on&clienteAcessoFTP=on&clienteAcessoDownloadBackup=on" -k --digest -u MY_USER:MY_PASSWORD -X POST API_URL`
    
    p output
    xml = Hash.from_xml(output) #in my case it returns a XML string
    

    Now output contains the response