我想下面的curl命令行到红宝石路沿石自动化:做一个帖子用Ruby遏制基本身份验证
curl -H "Content-Type:application/json" -X POST -d \
'{"approvalType": "auto",
"displayName": "Free API Product",
"name": "weather_free",
"proxies": [ "weatherapi" ],
"environments": [ "test" ]}' \
-u myname:mypass https://api.jupiter.apigee.net/v1/o/{org_name}/apiproducts
凡MYNAME,为mypass和{组织名称}是运行脚本填充之前。
我不知道如何使用Ruby Curb使用基本身份验证来执行带有JSON负载的http post。我尝试了以下方法:
require 'json'
require 'curb'
payload = '{"approvalType": "auto",
"displayName": "Test API Product Through Ruby1",
"name": "test_ruby1",
"proxies": [ "weather" ],
"environments": [ "test" ]}'
uri = 'https://api.jupiter.apigee.net/v1/o/apigee-qe/apiproducts'
c = Curl::Easy.new(uri)
c.http_auth_types = :basic
c.username = 'myusername'
c.password = 'mypassword'
c.http_post(uri, payload) do |curl|
curl.headers["Content-Type"] = ["application/json"]
end
puts c.body_str
puts c.response_code
结果是一个空的正文和一个415响应代码。我证实了curl命令完美无缺。
任何帮助将不胜感激,因为它可以解决我现在正在处理的一整类问题。
curl有-vv命令来显示详细的输出,你可以尝试相同的路边? – poseid 2013-05-08 16:20:19