# Setting up a simple Webservice - Why so hard?

**URL:** <https://www.ruby-forum.com/t/setting-up-a-simple-webservice-why-so-hard/138360>\
**Category:** Rails\
**Created:** [May 22, 2008, 10:19am UTC](https://www.ruby-forum.com/t/setting-up-a-simple-webservice-why-so-hard/138360 "2008-05-22T10:19:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rey9999](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@Rey9999](https://www.ruby-forum.com/u/Rey9999)\
**Post date:** [May 22, 2008, 10:19am UTC](https://www.ruby-forum.com/t/setting-up-a-simple-webservice-why-so-hard/138360/1 "2008-05-22T10:19:22Z")

</div>

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](http://manuals.rubyonrails.com/read/book/10)  
and then tweaked my apps to get them working in rails 2.0 as explained  
here:

> **[钛刻专家——官网](https://www.texperts.com/2007/12/21/using-action-web-service-with-rails-20/)**
>
> 钛刻专家致力于打造一个全球专家在线服务平台，为机构和个人对接高水平行业专家，为机构提供更系统性的线下定制专家服务。

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](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

---

<div class="post-metadata">

**Author:** ![Rey9999](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@Rey9999](https://www.ruby-forum.com/u/Rey9999)\
**Post date:** [May 22, 2008, 10:30am UTC](https://www.ruby-forum.com/t/setting-up-a-simple-webservice-why-so-hard/138360/2 "2008-05-22T10:30:32Z")

</div>

On May 22, 9:18 am, Rey9999 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Rey9999](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@Rey9999](https://www.ruby-forum.com/u/Rey9999)\
**Post date:** [May 22, 2008, 10:47am UTC](https://www.ruby-forum.com/t/setting-up-a-simple-webservice-why-so-hard/138360/3 "2008-05-22T10:47:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rey9999](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@Rey9999](https://www.ruby-forum.com/u/Rey9999)\
**Post date:** [May 22, 2008, 11:05am UTC](https://www.ruby-forum.com/t/setting-up-a-simple-webservice-why-so-hard/138360/4 "2008-05-22T11:05:19Z")

</div>

Ok, problem solved - I needed to set the options for  
#protect\_from\_forgery.

Thanks Fred, now it works like a charm!

Regards,

Rey9999
