我正在尝试将以下curl命令行自动化到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其中,在运行脚本之前填写了myname、mypass和{org name}。
我不知道如何使用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命令工作得很好。
任何帮助都将不胜感激,因为它将解决我现在正在解决的一大类问题。
发布于 2013-11-19 16:04:36
我使用Curb (0.8.5)并发现,如果我在多个请求上重用curl实例(get请求保存cookie,然后post数据),http_post方法的用法如下
http_post(uri, payload) 它实际上会将uri和有效负载组合到单个json请求中(这当然会导致错误,如“意外字符('h'...”或者“坏请求”)。
我设法让它正常工作,但我必须使用带有有效负载的方法作为单个参数:
c =Curl::Easy.new
url = "http://someurl.com"
headers={}
headers['Content-Type']='application/json'
headers['X-Requested-With']='XMLHttpRequest'
headers['Accept']='application/json'
payload = "{\"key\":\"value\"}"
c.url = url
c.headers=headers
c.verbose=true
c.http_post(payload)希望这能有所帮助。
发布于 2013-05-09 00:21:02
响应代码为415,表示“服务器不支持媒体类型”。如果您将Content-Type设置为这样(不带括号),是否有效?
curl.headers["Content-Type"] = "application/json"https://stackoverflow.com/questions/16445384
复制相似问题