HTTParty, Rails & Sinatra: Same (get) request, different response (behaviour)

393 views Asked by At

I've written a ruby gem that uses httparty to make various http requests. It works from irb and in rails apps but I am getting a very weird behaviour when I try to use it in a sinatra one.

The gem looks like this

gem:

require "httparty"
class SomeAPI::Client
 def initizialize(some_params)
   @api_params = some_params
 end

 def login
   login_response = HTTParty.get(@api_params[:login_url])
   puts "Login Result: #{login_response}"
 end
end

and when I try to login from irb everything is fine.

Then I include it on my rails app gemfile (gem "SomeAPI", :path => "~/dev/SomeAPI"), setup an action in a controller and trying to login from 0:0:0:0:3000/someapi?api_params=my_params works fine.

class SomeController < ApplicationController
  def someapi
    client = SomeAPI::Client.new(params[:api_params])
    client.login
  end
end

Finally, I try to do the same thing from my sinatra app: I include it in the gemfile and I try to make the request from 0:0:0:0:9292/someapi/:my_params. However, although, the new works fine (client becomes a SomeAPI object with all the attributes initizialized properly), the login gives me a You need to sign in or sign up before continuing. error message; as if I am trying to access a different resource (or as if login itself needs me to be authenticated beforehands to succeed). Here is the code:

class App < Sinatra::Base
  get "/someapi/:my_params" do
    client = SomeAPI::Client.new
    client.login
  end
end

I tried running the sinatra app from port 3000 (the same as the rails' default) but nothing changed.

Any ideas or leads on where to look at?

1

There are 1 answers

0
sebkkom On BEST ANSWER

I solved my problem by adding the below code to the login method:

params =
  {
    :headers => { "Content-Type" => "application/xml", "Accept" => "application/xml"},
    :basic_auth => { :username => "some_username", :password => "some_password" }
  }

(and of course changing the request to HTTParty.get(@api_params[:login_url], params))