# Calling a ruby program from java script

**URL:** <https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258>\
**Category:** Ruby\
**Created:** [December 27, 2007, 5:27am UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258 "2007-12-27T05:27:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pradeepta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pradeepta](https://www.ruby-forum.com/u/pradeepta)\
**Post date:** [December 27, 2007, 5:27am UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258/1 "2007-12-27T05:27:46Z")

</div>

HI ,

```
       How can i call a ruby program from java script . I want to

```

run a ruby program that will post form data on button click .And also  
how can I sent username , password form field’s data to a servlet  
?Anybody plz help !!!

---

<div class="post-metadata">

**Author:** ![pradeepta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pradeepta](https://www.ruby-forum.com/u/pradeepta)\
**Post date:** [December 27, 2007, 7:32am UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258/2 "2007-12-27T07:32:13Z")

</div>

This is how i used once…

:onchange=\>remote\_function(:url =\>{:action =\>  
‘update\_weekdays’,:controller=\>‘calenders’},:with =\> “‘id=’+this.value”,  
:method=\>:post)

---

<div class="post-metadata">

**Author:** ![pradeepta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pradeepta](https://www.ruby-forum.com/u/pradeepta)\
**Post date:** [December 27, 2007, 8:35am UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258/3 "2007-12-27T08:35:54Z")

</div>

I’d really like to see the implementation of ruby in your JScript. If  
it’s  
not too much trouble, would you mind upping it on MediaFire or something  
of the likes? I had dabbled with embedded Ruby before but I was never  
successful. (Woes of the newbie I guess. 🙂

On Wed, 26 Dec 2007 22:32:13 -0800, Ratnavel S.

---

<div class="post-metadata">

**Author:** ![pradeepta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pradeepta](https://www.ruby-forum.com/u/pradeepta)\
**Post date:** [December 27, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258/4 "2007-12-27T21:03:37Z")

</div>

Pradeepta S. wrote:

> My task is to automate the web service…  
> when someone opens the client’s site and enters credentials and  
> clicks button , then ruby script will pick that information from the  
> fields and after validating redirects to a 3rd party .

Pradeepta,

it is not clear yet what the ‘client site’ (that I will call simply  
‘client’ from now on) is:

1. is it the browser? (you mentioned ‘calling from javascript’ before).  
In this case, as far as I know, you cannot call a ruby program (resident  
in the same computer) from Javascript running in the browser.

2. is the ‘client’ a Gui tool that needs to invoke a program? depending  
on the platform you are in, there are many possibilities; from a  
rudimentary ‘system’ call (on Unix) to a socket-based communication  
(that would allow the ’ ‘ruby program’ communicate the ‘client site’  
information beyond success/failure).

This would mean that the ‘client’ has to serialize the ‘form’ (with the  
user data), and transmit the information to the ‘ruby program’ using  
whatever IPC is available (and best for the purpose).

Once we are inside the ruby program, you can use Net::HTTP. I don’t have  
direct experience with it, but a quick look at the doc shows several  
ways to do a POST request; for example:

url = URI.parse(’…’)  
req = Net::HTTP::Post.new(url.path)  
req.basic\_auth(…)  
req.set\_form\_data({‘user\_name’=\>’…’, ‘password’=\>’…’}, ‘;’)

# then the request is sent, etc

In any case, things seem simple; the original ‘form’ is translated into  
a hash of key-values (as usual). Of course you have to use the same  
names expected by server; but there is no problem in how to ‘transmit  
the form’ (that seems a concern of yours).

[You can also look at another library: rest-open-uri (a Ruby gem), that  
operates at a higher level than net/http; but this is not your problem  
right now].

The point to solve is how the ‘client’ serializes the form (again, not a  
problem for a form so simple, whatever language you are using in the GUI  
client) and in how you choose to do the IPC.

However the fact that you are so concerned with ‘how to process the  
form’ makes me think that something fundamental is still not clear yet  
in all this.

I hope this helped however

Raul

---

<div class="post-metadata">

**Author:** ![pradeepta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pradeepta](https://www.ruby-forum.com/u/pradeepta)\
**Post date:** [December 27, 2007, 8:48am UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258/5 "2007-12-27T08:48:59Z")

</div>

Pradeepta S. wrote:

> ```
> How can i call a ruby program from java script . I want to
> 
> ```
> 
> run a ruby program that will post form data on button click .And also  
> how can I sent username , password form field’s data to a servlet  
> ?Anybody plz help !!!

Pradeepta,

I read the 3 postings (with different titles) that you made recently  
on this subject. In the previous post, you mentioned ‘req.set\_form\_data’  
(like you were writing a web service client), now you say that you are  
‘in javascript’.

It would help if you clarified this: where is the ‘ruby program’ that  
you are trying to call? I have the suspicion that you are trying to call  
a ruby program that is in the computer where the browser is (and you  
cannot do that).

Or do you mean that the ‘ruby program’ is in the server? in this case  
you should just let javascript submit the form (specifying an ‘action’,  
the method post, the input fields for user name and password, and the  
submit button; there is no ‘programming’ to do). On the server side (I  
assume that you are not using Rails), you write a simple Ruby program  
which uses CGI to collect the form parameters (and that is presumably  
the same program that sent the form).  
However I see that in the first post you mentioned a (Java?) ‘servlet’,  
so it would seem that the server is already taken care of…

I just wanted to explain why you are getting different answers every  
time, as everyone is giving a different interpretation to your question.

Raul

---

<div class="post-metadata">

**Author:** ![pradeepta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pradeepta](https://www.ruby-forum.com/u/pradeepta)\
**Post date:** [December 27, 2007, 11:07am UTC](https://www.ruby-forum.com/t/calling-a-ruby-program-from-java-script/124258/6 "2007-12-27T11:07:24Z")

</div>

Hi Raul,  
Thanx 4 ur suggestion . Actually i m a newbie to ruby and  
found it very exciting as I m a java developer ,working 4 an IT MNC . My  
task is to automate the web service that we r offering 2 a client.my  
tasks are :

1. when someone opens the client’s site and enters credentials and  
clicks button , then ruby script will pick that information from the  
fields and after validating redirects to a 3rd party .

2.Another script will receive response from client’s site in JSON format  
and validates it .

3.Third script checks for IP based authentication

My problem is the first one as I don’t want to use rails and using pure  
ruby I want to process form data and forward it to server upon the click  
or enter event . can u please help me out ?

Thanks & Regards

Pradeepta
