Set authorization in the header

47 views
Skip to first unread message

Anthony Roux

unread,
Aug 2, 2011, 9:28:46 AM8/2/11
to restfulie
Hello,

I would like to be able to set my own authorization in the request
header with Restfulie.
I would like to do something like that:

@response = Restfulie.at(url).accepts('application/
xml').authorization(@authorization).get

But i can't find the good syntax, can you help me ?
The other thing that i want to do it is to set the authorization in
this type of request:

response = family_index.links.self.follow.get

How i have to do ?
Maybe it is possible with Restfulie to set the authorization one time
for all the futur requests ?

Regards,
Anthony.

Guilherme Silveira

unread,
Aug 2, 2011, 1:17:27 PM8/2/11
to rest...@googlegroups.com

Hi Anthony

Lets solve the first and then move for the second.

For the first one ou can create a request feature. Take a look at any of the feature examples on the source code. You can also see the doc on how to register it.

Can you let mr know if it solves the first?

The second one means keepibg the variable on the response so any links that are created will use it. You can take a look at the response features too.

Regards

Guilherme Silveira

Anthony Roux

unread,
Aug 3, 2011, 9:15:40 AM8/3/11
to rest...@googlegroups.com
Hello,

I don't understand why is so simple to set "accept", and why i have to define a custom feature to set the authorization ?

And i have tried to reproduce the same thing, but i have some problem.
"def execute(chain, request, env={})" What is the "chain" parameter ?

If i want to create my own class (into the module Restfulie::Client::Feature) I saw that i have to use it with "Restfulie.at(uri).my_feature.get" but how I can set the request parameter ? Can i do something like that :

my_feature = Restfulie::Client::Feature.MyFeature.new @req = http.get("/", {'Authorization' => @authorization, 'Content_type' => @content_type, 'Accept' => @accept}) @chain = mock Restfulie::Client::StackNavigator @response = my_feature.execute(@chain, @req, {})

Anthony.

2011/8/2 Guilherme Silveira <guilherme...@caelum.com.br>

Anthony Roux

unread,
Aug 5, 2011, 11:51:01 AM8/5/11
to rest...@googlegroups.com
Hello,

I created "my_feature.rb" : 

module Restfulie::Client::Feature
  class MyFeature

        def execute(chain, request, env)

            request.default_headers["Authorization"] = @authorization
            # dispatch the request
            response = chain.continue(request, env)
        end
  end
end

In my home controller i want to use it with :

@response = Restfulie.at("http://55.7.28.210:4321/TSI-Dev/test_systems").my_feature.get

Currently my file is located at : ".../ruby/1.9.1/gems/restfulie-1.0.0/lib/restfulie/client/feature/my_feature.rb" but i always have an 

   TypeError in HomeController#index

wrong argument type Class (expected Module)

Maybe i need a special require ? Maybe the file isn't at the good place ? I tried lot of things (with docs and source code) but the only thing that works is to modify base.rb to add authorization method...

I think i have missed something important, maybe someone can give me an advice.

Thanks.

2011/8/3 Anthony Roux <m.ro...@gmail.com>

Guilherme Silveira

unread,
Aug 6, 2011, 4:38:58 PM8/6/11
to rest...@googlegroups.com

Hi Anthony

If you just want to set the header by hand you can do it by.just adding the header.

Ill take a look at your code now.

Guilherme Silveira

On Aug 3, 2011 6:15 AM, "Anthony Roux" <m.ro...@gmail.com> wrote:
> Hello,
>
> I don't understand why is so simple to set "accept", and why i have to
> define a custom feature to set the authorization ?
>
> I have looked this documentation:
> https://github.com/caelum/restfulie/wiki/ruby_features
> And i have tried to reproduce the same thing, but i have some problem.
> "def execute(chain, request, env={})" What is the "chain" parameter ?
>
> If i want to create my own class (into the
> moduleRestfulie::Client::Feature) I saw that i have to use it with

Anthony Roux

unread,
Aug 8, 2011, 4:29:48 AM8/8/11
to rest...@googlegroups.com
Hello,

If i create my header

 @head = {
      'Accept' =>'text/xml',
      'Content_type' => 'text/xml',
      'Authorization' => session[:auth]
      }

And i use it like this:
@response  = Restfulie.at("http://55.7.28.210:4321/TSI-Dev/test_systems").with(@head).get

When i show the response i have an: undefined method `strip' for nil:NilClass and i don't understand why...

Anthony.

2011/8/6 Guilherme Silveira <guilherme...@caelum.com.br>

Anthony Roux

unread,
Aug 8, 2011, 8:28:55 AM8/8/11
to rest...@googlegroups.com
Hello,

Ok after some research on your code i found something strange in client/feature/base.rb

    # Merge internal header
    #
    # * <tt>headers (e.g. {'Cache-control' => 'no-cache'})</tt>
    #
    def with(headers)
      headers.merge!(headers)
      self
    end

You merge headers by himself.

self.headers.merge!(headers)
will be better.

A method that can offer the possibility to set authorization can be a good thing ? But with "with"  methodi agree that it is not an obligation.

in client/feature/base.rb

def authorization(auth)
      headers['Authorization'] = auth
      self
end


But i still working on the feature...

Anthony.

2011/8/8 Anthony Roux <m.ro...@gmail.com>
Reply all
Reply to author
Forward
0 new messages