2013-03-23 29 views
0

所以,如果我执行下面的命令,它工作正常:麻烦与NET/HTTP POST数据

curl -X POST --compressed -H "Content-Type:application/json" \ 
    -H 'x-api-user: USER_ID' \ 
    -H 'x-api-key: API_TOKEN' \ 
    -d '{"text":"from the api!","type":"todo"}' \ 
    https://habitrpg.com/api/v1/user/task 

我试图让HTTParty工作,但他们对连接压缩文件基本上是不存在的,在我的理解水平。我尝试过使用NET/HTTP,并且可以建立连接,但是API不断给我提供错误,说明我的数据错误(它说我没有给它一个有效的类型)。这里是我使用的代码:

data = {:text => "from the app dog", :type => "todo"} 
uri = URI.parse("https://habitrpg.com/api/v1/user/task") 
http = Net::HTTP.new(uri.host, uri.port) 
http.set_debug_output($stdout) 
http.use_ssl = true 
resp = http.post(uri.request_uri, data, {"x-api-user" => USER_ID, "x-api-key" => API_TOKEN}) 
resp.body 

我也尝试了所有的数据如下:

data = {:text => "from the app dog", :type => "todo"}.to_json.to_s 
data = "{\"text\":\"from the app dog\",\"type\":\"todo\"}" 
data = "'" + "{\"text\":\"from the app dog\",\"type\":\"todo\"}" + "'" 

,也许三四个,我再也不能识别其他的事情。有没有人有什么建议?我处于全面亏损状态。

回答

0

您的curl命令具有内容类型集。您发布的任何Ruby代码都没有。

我猜应用程序无法检测到并解析给它的JSON。

+0

你不知道多么愚蠢,让我感觉。我认真地花了30分钟搞乱那串。 – 2013-03-23 22:23:16