2

我无法将 curl 转换为 ruby​​ 脚本。基本上我正在尝试使用 parse.com RESTful api。这是卷曲

curl -X POST \
-H "X-Parse-Application-Id: XXX" \
-H "X-Parse-REST-API-Key: YYY" \
-H "Content-Type: application/json" \
-d '{
   "channels": [
     "Giants",
     "Mets"
   ],
   "data": {
     "alert": "test message"
   }
 }' \
 https://api.parse.com/1/push

这就是我一直在尝试做的(使用 HttParty)

puts "hello world"

require 'httparty'

require 'json'

class Parse
    include HTTParty
    base_uri "api.parse.com:443"
    headers "X-Parse-Application-Id" => "XXX", "X-Parse-REST-API-Key" =>  "YYY", "Content-Type" => "application/json"

end

option = {:channels => "Giants", :data => {:alert => "un mensaje de mierda"}}
puts Parse.post("/1/push", body: option.to_json)

)

我收到错误代码 107 说无效的 json。任何建议将不胜感激。

4

1 回答 1

1

我认为您只需要在第二个参数中将 ruby​​ 结构序列化为 JSON。参数应该是 POST 的字符串,而不是 Ruby 结构。

我认为这会起作用(我能看到的唯一可能的问题是您是否会在编写代码时通过 https 进行连接):

data = {"channels": ['Giants'], "data": {alert: 'un mensaje '}}
puts Parse.post("/1/push", body: data.to_json)

. . . Ruby 数据结构中的类 JSON 格式不是JSON,

foo: "bar"

只是另一种 Ruby (1.9+) 的说法

:foo => "bar"
于 2013-03-14T08:07:25.443 回答