# Reading http request as IO in rails

**URL:** <https://www.ruby-forum.com/t/reading-http-request-as-io-in-rails/129135>\
**Category:** Rails\
**Created:** [February 14, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/reading-http-request-as-io-in-rails/129135 "2008-02-14T18:47:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 14, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/reading-http-request-as-io-in-rails/129135/1 "2008-02-14T18:47:45Z")

</div>

Hi,  
Here’s what i’m trying to do my mobile phone app ( j2me) tries to  
connect to the my rails application.  
the j2me app opens a java.io.OutputStream here’s the snippet  
hc = (HttpConnection) Connector.open(UPDATE\_URL,  
Connector.READ\_WRITE);  
hc.setRequestMethod(HttpConnection.POST);  
hc.setRequestProperty(“If-Modified-Since”, “29 Oct 1999  
19:43:31 GMT”);  
hc.setRequestProperty(“User-Agent”, “Profile/MIDP-2.0  
Configuration/CLDC-1.2”);  
hc.setRequestProperty(“Content-Language”, “en-US”);  
out = hc.openOutputStream();

j2me app then just writes data to the output stream(pretty basic  
here). so far so good. not a problem here.  
And on my rails controller i do the following,

class MainController \< ApplicationController  
def read\_data  
url = request.env[“REQUEST\_URI”]  
method = request.env[“REQUEST\_METHOD”]  
data = request.env[“RAW\_POST\_DATA”]

```
newf = File.open("MY_DATA", "wb")
str = request.body.read
newf.write(str)
newf.close
render :text => "Hello Load #{url}"

```

end  
end

the problem is the same j2me app works(only changes the url) if i have  
a servlet receiving the data, i do request.getInputStream on a servlet  
and everything works.  
But on rails i get ioexception under j2me app saying couldn’t write to  
socket.

Also is request.body.read the correct way to do it. ?  
Is this possible in rails or am i making the wrong assumptions here ?  
what am i doing wrong, or rather how is it done.

I appreciate any insights that you may have.  
mb

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 15, 2008, 4:49am UTC](https://www.ruby-forum.com/t/reading-http-request-as-io-in-rails/129135/2 "2008-02-15T04:49:51Z")

</div>

Anybody, any pointers.
