代码之家  ›  专栏  ›  技术社区  ›  brokenbeatnik

Rails1.x客户端与RESTful服务器对话

  •  0
  • brokenbeatnik  · 技术社区  · 16 年前

    嘿,我有一个客户机正在将Rails1.2.6站点与另一个RESTfully公开服务的站点集成。目前还不能升级到Rails2.x。是否有人建议使用直接Net::HTTP调用以外的方法与REST服务通信?技术或Gem建议是受欢迎的,但是我所看到的大多数Gem似乎都依赖于activesupport2.x,据我所知,activesupport2.x与rails1.x不兼容。

    提前感谢您提供的任何意见。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Chris Heald    16 年前

    尝试 HTTParty

        2
  •  0
  •   brokenbeatnik    16 年前

    # Assume @user_name and @password were previously declared to be the 
    # appropriate basic auth values and that the connection is open as @connection
      def put(path, body, header={})
        request = Net::HTTP::Put.new(path, header.merge({'Accept' => 'application/xml,application/json', 'Content-type'=>'application/json'}))
        request.basic_auth(@user_name, @password)
        @connection.request(request, body).body
      end
    
      def post(path, body, header={})
        request = Net::HTTP::Post.new(path, header.merge({'Accept' => 'application/xml,application/json', 'Content-type'=>'application/json'}))
        request.basic_auth(@user_name, @password)
        @connection.request(request, body).body
      end 
    
      def get(path, header={})
        request = Net::HTTP::Get.new(path)
        request.basic_auth(@user_name, @password)
        @connection.request(request).body   
      end
    

    然后,我对这些方法的输出调用了JSON::parse(),得到了一个表示JSON的哈希,我认为可以使用它。