简体   繁体   中英

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&clienteEmail=email@dominio.com.br&clienteEmailCobranca=emailcobranca@dominio.com.br&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?

Here is an (untested) example using rest-client that might get you started https://github.com/rest-client/rest-client ).

According to what I was able to find online, rest-client automatically supports the digest authentication that that curl request is doing.

require 'rest-client'
response = RestClient::Request.execute(
            method: :post,
            url: 'https://api.kinghost.net/cliente',
            verify_ssl: false,
            user: "usuario",
            password: "senha",

            headers: {
              params: {
                clienteTipo: "I",
                clienteCPFCNPJ: "00112135045",
                clienteEmpresa: "NomeEmpresa",
                clienteNome: "meunome",
                clienteEmail: "email@dominio.com.br",
                clienteEmailCobranca: "emailcobranca@dominio.com.br",
                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"
              }
            }
          )

the wonders of curl :)

-k means unsecure (i.e don't check SSL)
--digest is the authentication type
-u name:password is the actual username and password
-X POST it's a post
-d "blah blah" blah blah is the payload

now onto ruby: I would look at https://github.com/rest-client/rest-client or at https://github.com/jnunemaker/httparty It's just a matter of figuring out how to perform the post. The digest might be tricky but IIRC it can be some via Authorization header.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM