Net::http does not return what a browser returns

Hello,

I trying to access an API with ruby, but I can’t get net::http to get me
the response.

Here is the code :

http.start do |session|
response = session.get(’/__GetChallenge’)

result = Digest::SHA1.hexdigest response.body + password.upcase

result2 =
session.get("/login?Plugin=BFAPI&Templates=Session&Challenge=#
{response.body}&UserID=#{user}&SHA1=#{result}",{‘Cookie’ =>
response[‘Set-Cookie’]})
end

Now if I send these via a browser (chrome) result2 comes out with a
string, although in here result2 has a body of length 0.

The initial response works fine.

Any ideas? Thanks.

Are you sure you do not need any other cookies?

Are you sure it’s not HTTPS?

Maybe you should parameter-encode the parameters (e.g.
UserID=#{CGI.escape user} instead of UserID=#{user})?

Are you calculating the SHA1 correctly (maybe you should strip
whitespace off the body)?

These just off the top of my head. Also, we could help you better if
you provided the URL of the API. Also, consider using RestClient or
some other sensible means of consuming APIs (standard Net::HTTP is
quite low-level).

– Matma R.

I’m sure everything works fine. Via the browser I have tried the actual
gets and those work. I also just tried it with the mechanize gem and got
it to work the way I wanted. This is the code I used with mechanize :

agent = Mechanize.new

challenge = agent.get(site_url + ‘__GetChallenge’)
result = Digest::SHA1.hexdigest(challenge.body + password.upcase)

login = agent.get(site_url +
“login?Plugin=BFAPI&Templates=Session&Challenge=#{challenge.body}&UserID=#{user_id}&SHA1=#{result}”)

This code returns the correct information.

I can’t see any difference…

I’m also using https at the moment if that makes a difference.

I’ll also check out RestClient.

Thanks

I’ve looked at Rest and found two gems that could work. Which one would
you suggest :
rest-client - GitHub - archiloque/rest-client: A link to the new repository
restfulie - http://restfulie.caelum.com.br/