Forum: Ruby NET:HTTP headers ruby gem

Posted by Shaban K. (shaban_k)
on 2012-10-05 08:43
I am trying to use the NET:HTTP gem to add an api-key to http header of
a client, but it just doesn't seem to be working for some reason when I
try and test it out.Basically the server requires the http header of the
client or anything to have http_x_api header in order to serve the
request.

Server code

   require 'sinatra'

   before do
     halt 400 if (env['API_KEY']) != 'wow'
  end

   get '/' do
     "boo"
   end

client code
    require 'net/http'
    require 'uri'

    port = ENV['PORT'] || '7474'

    res = Net::HTTP.start('localhost', port )  { |h| h.get('/')}
    res.add_field('api-key', 'wow')
    res.each_header do |key, value|
      p "#{key} => #{value}"
    end
    puts (res.code == '200' && res.body == 'boo') ? 'OK' : 'FAIL'

    this the response i get back :=>

   "x-frame-options => sameorigin"
   "x-xss-protection => 1; mode=block"
   "content-type => text/html;charset=utf-8"
   "content-length => 0"
   "connection => keep-alive"
   "server => thin 1.5.0 codename Knife"
   "api-key => wow"
    FAIL
Posted by Ryan Davis (Guest)
on 2012-10-05 09:33
(Received via mailing list)
On Oct 4, 2012, at 23:43 , Shaban K. <lists@ruby-forum.com> wrote:

> I am trying to use the NET:HTTP gem to add an api-key to http header of
> a client, but it just doesn't seem to be working for some reason when I
> try and test it out.Basically the server requires the http header of the
> client or anything to have http_x_api header in order to serve the
> request.

By "to http header of a client" I assume you mean to the request...

>
>    res = Net::HTTP.start('localhost', port )  { |h| h.get('/')}

    ^^^ response                                     ^^^^^^^^^^ request

and everything below is response as well...
Posted by Shaban K. (shaban_k)
on 2012-10-05 12:15
Yeah that is what I mean
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.