我想下面的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