Consuming non-REST APIs in Rails with ActiveResource

前端 未结 3 536
眼角桃花
眼角桃花 2021-02-04 20:48

I\'m writing a client that consumes a non-REST API (i.e. GET site.com/gettreasurehunts), which requires that I specify all parameters (even the resource ID) in the request\'s HT

3条回答
  •  被撕碎了的回忆
    2021-02-04 21:28

    I would recommend HTTParty, it's pretty flexible and I'm sure capable of handling what you need.

    Some examples from the project:

    pp HTTParty.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544')
    pp HTTParty.get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => 46544})
    
    @auth = {:username => u, :password => p}
    options = { :query => {:status => text}, :basic_auth => @auth }
    HTTParty.post('http://www.twitter.com/statuses/update.json', options)
    

    And if you need to POST something in the body of the request, simply add :body => "text" to the options hash.

    It's very straightforward to work with and I'm currently using it in place of ActiveResource to consume some REST services from a Rails app.

提交回复
热议问题