如何在 Ruby 上执行这种 CURL 请求
Posted
技术标签:
【中文标题】如何在 Ruby 上执行这种 CURL 请求【英文标题】:How to perform this kind of CURL request on Ruby 【发布时间】:2015-07-08 20:59:35 【问题描述】:我正在使用我的网络主机提供的 API,我可以通过该 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,但我想知道我必须如何通知这些参数,它们看起来像一个查询字符串。这个摘要是什么?
【问题讨论】:
docs.ruby-lang.org/en/2.0.0/Net/HTTP.html 【参考方案1】:这是一个使用 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"
)
【讨论】:
【参考方案2】:卷曲的奇迹:)
-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-client 或https://github.com/jnunemaker/httparty 这只是弄清楚如何执行帖子的问题。摘要可能很棘手,但 IIRC 它可以是一些通过授权标头。
【讨论】:
【参考方案3】:在挣扎了一段时间并测试了不同的库之后,我使用 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
现在输出包含响应
【讨论】:
有时最简单的就是最好的!很高兴您找到了解决方案。以上是关于如何在 Ruby 上执行这种 CURL 请求的主要内容,如果未能解决你的问题,请参考以下文章
如何将此 curl 命令转换为 Ruby rest-client put 请求?