How to Get Sinatra to Work with Httpclient

How do I get Sinatra to work with HTTPClient?

I found the reason. HTTPClient defines a module named HTTP. By default Sinatra looks for Rack handlers with namespace names HTTP and WEBrick, in that order.

Since the HTTP namespace has been defined Sinatra actually thinks it's a Rack handler. I think this is a bug in Sinatra. It should check if the handler responds to run before using it.

Anyway the fix is to use Thin, or if you want to use WEBrick then explicitly tell Sinatra to skip the automatic Rack detection by doing:

set :server, 'webrick'

That will prevent Sinatra from thinking the HTTPClient HTTP module is Rack handler.

Ruby Http gem (http client) inside Sinatra POST

Changes to

post "/weather" do
puts HTTP.get('https://www.metaweather.com/api/location/search/?query=milwaukee') # prints correct result
HTTP.get('https://www.metaweather.com/api/location/search/?query=milwaukee').to_s
end

The HTTP.get method returns a HTTP::Response object rather than a String object.


From the source code only specific types of object can be returned.

 res = [res] if Integer === res or String === res
if Array === res and Integer === res.first
res = res.dup
status(res.shift)
body(res.pop)
headers(*res)
elsif res.respond_to? :each
body res
end
nil # avoid double setting the same response tuple twice

Ruby/Sinatra OAuth implementation

You're not doing anything wrong. That's exactly the standard output you'll get from the service when you run it - if you then go to http://localhost:1234 on that same machine you should see a "Sinatra doesn't know this ditty" message, which is also correct.

Once running like this, you'll be able to use it with the iOS SDK. However, since it's on localhost it won't be accessible from your iOS device - you'll need to put it in a publicly accessible location for that.

How do I access a web page from Sinatra?

Open-URI will do a good job if all you need to do is retrieve the content of the URL or tickle some job at the other end of the URL.

If you need more control then Net::HTTP or Typhoeus are available.

Both Open-URI and Net::HTTP are part of Ruby's standard installation.

I just want to add further that "HTTParty" is a very good wrapper around "Net::HTTP" (see httparty.rubyforge.org)

Yes, I agree. It's nicely done. I reach for OpenURI first since it's built-in. HTTParty is lighter-weight than Typhoeus so it's nicely positioned between them.

Ruby/Sinatra app (file) does not run (start)

I just needed to add this code, and my sinatra app is now alive:

set :server, 'webrick'


Related Topics



Leave a reply



Submit