如何在Ruby上执行这种CURL请求

时间:2015-07-08 20:59:35

标签: ruby-on-rails ruby curl digest digest-authentication

我正在使用我的网络托管服务商提供的API,我可以通过它注册新的客户端和域名。不幸的是,他们没有提供太多的文档(基本上没有),我不太习惯cURL

他们给出了一个非常肤浅的例子来说明如何创建一个新的客户端,如下所示

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

如何在Ruby上的cURL请求中对此进行转换,保持参数如上所述?

这必须是POST,但我想知道如何通知这些参数,它们看起来像一个查询字符串。那摘要是什么东西?

3 个答案:

答案 0 :(得分:2)

这是一个使用rest-client的(未经测试的)示例,可能会让您入门https://github.com/rest-client/rest-client)。

根据我在网上找到的内容,rest-client自动支持curl请求正在进行的摘要式身份验证。

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"
              }
            }
          )

答案 1 :(得分:1)

卷曲的奇迹:)

-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

现在到红宝石: 我会看https://github.com/rest-client/rest-clienthttps://github.com/jnunemaker/httparty 这只是弄清楚如何执行帖子的问题。摘要可能很棘手,但IIRC可以通过授权标题进行。

答案 2 :(得分:0)

经过一段时间的努力并测试不同的库后,我使用Ruby来运行系统命令,因为cURL不依赖于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

现在输出包含响应