I am trying to automate the following curl command line into Ruby Curb:
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
Where myname, mypass and {org name} are filled in prior to running the script.
I don't know how to do a http post with the JSON payload using basic authentication using Ruby Curb. I tried the following:
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
The result is an empty body and a 415 response code. I verified that the curl command works perfectly fine.
Any help would be greatly appreciated because it would solve a whole class of problems I'm working on now.