Restful_authentication as a web service?

Hey everyone,

I have been using Ruby on Rails for a few months now (I come from a
php / Java/J2EE background) and have been LOVING it. I do have one
question regarding XML output and the SessionsController#create method
that is generated by the restful authentication plugin.

I added the following code to the create method:

  #if login is successful

  respond_to do |format|
    format.html { redirect_back_or_default('/')}# index.html.erb
    format.xml  { render :xml => self.current_user }
  end


#if login is not successful

  respond_to do |format|
    format.html { render :action => 'new'}# index.html.erb
    format.xml  { response.status_code.should == 401  }
  end

When I try to test this like this:

http://localhost:3000/sessions/create?login=myuser&password=mypassword
I am able to login successfully. If I add .xml to the end of create
like this:
http://localhost:3000/sessions/create.xml?login=myuser&password=mypassword
I get the following exception: No route matches “/sessions/create.xml”
with {:method=>:get}. My routes.rb is pretty generic (has all of the
restful authentication mappings) and I haven’t touched it. Any ideas
as to what I’m doing wrong? How can I log in to my site from a 3rd
party client?

Thanks,
Mark Dinstuhl

The format is determined by the HTTP Accept header, and not by adding
an extension on the URL.
So, your url remains /sessions/create and not /sessions/create.xml

You can test this out by using a command line tool like curl. For
example:

curl -H “Accept: application/xml” “http://localhost:3000/sessions/
create?login=xxx&password=xxx”

This should return your xml.

You can use the RestfulClient gem to test it out instead of CURL.

hitch wrote:

The format is determined by the HTTP Accept header, and not by adding
an extension on the URL.
So, your url remains /sessions/create and not /sessions/create.xml

Wait a moment. The URI is actually /sessions not /sessions/create. The
HTTP verb is used to select the create method: POST
http://localhost:3000/sessions would route the request to the create
method in the controller.

Larry,

Thank you SO much for answering my question. If anyone is
wondering, THIS WORKED.

Have a good one!
Martin Dinstuhl

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs