Setting up a simple Webservice - Why so hard?

Hello everyone.

I want to write a simple webservice in rails, which will accept
requests by a javascript client using XML-RPC.
Knowing very little about webservices in general, I decided first to
set up a couple of simple applications (a server and a client) to
learn how webservices work.
Using rails 2.0, I first followed the instructions here:
http://manuals.rubyonrails.com/read/book/10
and then tweaked my apps to get them working in rails 2.0 as explained
here:

Now, please follow me and have a look if I’m doing something wrong,
'cause I cannot get a single thing working…

I set up a single method, which takes a string and returns an integer
(it does nothing - it’s just to see if it works):

(content of /app/apis/myws_api.rb)
class MywsAPI < ActionWebService::API::Base
inflect_names false

api_method :givenumber, :expects => [:parameter1=>:string], :returns
=> [:int]

end

(content of /app/controllers/myws_controller.rb)
class MywsController < ApplicationController

web_service_dispatching_mode :layered
web_service :myws, MywsService.new

end

(content of /app/models/myws_service.rb)
class MywsService < ActionWebService::Base
web_service_api MywsAPI

def givenumber(:parameter1)
    return   1
end

end

And that should be all, right?
Now for the client… I just really wanted just to see something
coming out of the webservice in the quickiest and dirtiest way, so I
just scaffolded a Thing object. In its edit action, I just call the
web service and flash it out:

(excerpt from thing_controller.rb)

def edit

@myws_client = ActionWebService::Client::XmlRpc.new(MywsAPI,

http://localhost:3000/myws/api’, :handler_name => ‘myws’)
flash[:notice] = @myws_client.givenumber(‘test’)
redirect_to(things_path)

end

When I fire things up, something goes wrong. My client gets a most
discouraging:
“HTTP-Error: 500 Internal Server Error”
While mongrel says that there has been a:
“LoadError (Expected [my project path]/app/apis/myws_api.rb to define
MywsApi)”

Since MywsApi is defined in myws_api.rb, I do not really know what’s
happening here.
I know that the client ‘sees’ the server, because if I try to call the
service with the wrong parameters, example:
@myws_client.givenumber(‘test’, ‘test2’)
I get the error:
givenumber: wrong number of arguments (2 for 1)

So, where am I messing up?
Any help would be much appreciated - Thanks in advance!

Regards,
Rey9999

On May 22, 9:18 am, Rey9999 [email protected] wrote:

(content of /app/models/myws_service.rb)
class MywsService < ActionWebService::Base
web_service_api MywsAPI

def givenumber(:parameter1)
    return   1
end

end

That is syntactically incorrect. It should be def
givenumber(parameter1). In addition for magic autoload to work, you
need to follow the rails naming conventions, so MywsAPI should be
MywsApi (see the error message ?)

Fred

Thanks for the quick reply!

But now I get another strange error:
HTTP-Error: 422
ActionController::InvalidAuthenticityToken
(ActionController::InvalidAuthenticityToken)
/vendor/rails/actionpack/lib/action_controller/
request_forgery_protection.rb
:79:in `verify_authenticity_token’
…and so on…

Do I need to configure something to let the webservice run?

Thanks

Rey9999

Ok, problem solved - I needed to set the options for
#protect_from_forgery.

Thanks Fred, now it works like a charm!

Regards,

Rey9999