# Sending a fire&forget http request in a model class

**URL:** <https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626>\
**Category:** Rails\
**Created:** [November 19, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626 "2008-11-19T15:23:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 19, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626/1 "2008-11-19T15:23:11Z")

</div>

I know this is bad mvc, but i have a model class that needs to send a  
request off to a web service. At the moment i’m building a request  
string with url and params, and then doing a ‘system’ call to call curl  
with the url. This feels pretty dirty though. Is there a nicer way to  
just fire off an http request?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 19, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626/2 "2008-11-19T15:37:26Z")

</div>

On 19 Nov 2008, at 14:23, Max W. wrote:

> I know this is bad mvc, but i have a model class that needs to send a  
> request off to a web service. At the moment i’m building a request  
> string with url and params, and then doing a ‘system’ call to call  
> curl  
> with the url. This feels pretty dirty though. Is there a nicer way  
> to  
> just fire off an http request?

net/http or similar ?

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 20, 2008, 2:13am UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626/3 "2008-11-20T02:13:19Z")

</div>

Hey,  
I would check this out:  
[http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html)

Syntax looks something like:

http = Net::HTTP.new(“[www.google.com](http://www.google.com)”)  
headers, body = http.get()

Hope that help  
Jim Englert  
[http://www.jim-rants.com/coding-blog/](http://www.jim-rants.com/coding-blog/)

On Wed, Nov 19, 2008 at 9:36 AM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 20, 2008, 11:15am UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626/4 "2008-11-20T11:15:19Z")

</div>

On 20 Nov 2008, at 09:38, Max W. wrote:

> l\_content=‘[http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==](http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==)’  
> -d l\_email=‘removed\_email\_address@domain.invalid’ \
> 
> http = Net::HTTP.new(“#{domain}”)  
> headers, body = http.get(“#{path}?#{params}”)

Yuck. would be a lot easier to take a hash of params (ie {‘l\_cid’ =\>  
‘1106’, ‘l\_key’ =\> ‘cg3608898b74a4c688787ab479b8bb9f’, …} and then  
call to\_query on it.

Net::HTTP will also do that for you, with something like

response = Net::HTTP.start(host, port) do |http|  
get = Net::HTTP::Get.new path  
get.form\_data = some\_hash  
get.content\_type = “application/x-www-form-urlencoded”  
http.request get  
end

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 20, 2008, 10:38am UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626/5 "2008-11-20T10:38:25Z")

</div>

Thanks Fred

I’m not having any luck with it so far - this is the curl call i was  
making (with some changes to protect the innocent):

curl [http://www.l-mail.biz/scripts/lia/lia.php](http://www.l-mail.biz/scripts/lia/lia.php)   
-d l\_cid=‘1106’   
-d l\_key=‘cg3608898b74a4c688787ab479b8bb9f’   
-d  
l\_content=‘[http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==](http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==)’  
  
-d l\_pb\_url=‘[http://ir.chazanga.com/letter/confirm](http://ir.chazanga.com/letter/confirm)’   
-d l\_sname=‘chazanga’   
-d l\_rname=‘Max W.’   
-d l\_rcname=‘chazanga’   
-d l\_raddress1=‘Suites 17 & 18’   
-d l\_raddress2=‘9-12 Middle Street’   
-d l\_rcity=‘Brighton’   
-d l\_rpostcode=‘BN1 3TN’   
-d l\_rcountry=‘3’   
-d l\_email=‘removed\_email\_address@domain.invalid’   
-d l\_fb\_emails=‘1’   
-d p\_user\_id=‘2054’

So, when i tried to use Net::HTTP i was trying to deal with this massive  
params string. I split it up like so:

domain = “www.l-mail.biz”

path = “/scripts/lia/lia.php”

params =  
“l\_cid=1106&l\_key=cg3608898b74a4c688787ab479b8bb9f&l\_content=[http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==&l\_pb\_url=http://ir.chazanga.com/letter/confirm&l\_sname=chazanga&l\_rname=Max-Williams&l\_rcname=chazanga&l\_raddress1=Suites-17-18&l\_raddress2=9-12-Middle-Street&l\_rcity=Brighton&l\_rpostcode=BN1-3TN&l\_rcountry=3&l\_email=removed\_email\_address@domain.invalid&l\_fb\_emails=1&p\_user\_id=2054](http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==&l_pb_url=http://ir.chazanga.com/letter/confirm&l_sname=chazanga&l_rname=Max-Williams&l_rcname=chazanga&l_raddress1=Suites-17-18&l_raddress2=9-12-Middle-Street&l_rcity=Brighton&l_rpostcode=BN1-3TN&l_rcountry=3&l_email=removed_email_address@domain.invalid&l_fb_emails=1&p_user_id=2054)”

The params string had spaces and & signs in it, so i just replaced them  
with safe characters temporarily while i played with Net::HTTP (with  
escaping them properly down for a later task).

I tried this:

http = Net::HTTP.new(“#{domain}”)  
headers, body = http.get(“#{path}?#{params}”)

and got a 401 (Authorisation required) error back. I think maybe my  
params weren’t going through properly.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 20, 2008, 11:57am UTC](https://www.ruby-forum.com/t/sending-a-fire-forget-http-request-in-a-model-class/153626/6 "2008-11-20T11:57:36Z")

</div>

Thanks Fred…

I totally agree, i actually build up all the params in a hash anyway.

Here it is in your recommended format. One of the params has the  
complication of actually being a full url with params itself. Curl  
seems to deal happily with this, i’m guessing it escapes everything  
nicely. But trying this, i get the same 401 error back  
(#\<Net::HTTPUnauthorized 401 Authorization Required readbody=true\>).

host = “www.l-mail.biz”  
port = 80  
path = “/scripts/lia/lia.php”

param\_hash = {  
:l\_cid =\> ‘1106’,  
:l\_key =\> ‘cg3608898b74a4c688787ab479b8bb9f’,  
:l\_content =\>  
‘[http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==](http://ir.chazanga.com/letter/show?encoded=MTE4MiZpdCdzIHdlZG5lc2RheQ==)’,  
:l\_pb\_url =\> ‘[http://ir.chazanga.com/letter/confirm](http://ir.chazanga.com/letter/confirm)’,  
:l\_sname =\> ‘chazanga’,  
:l\_rname =\> ‘Max W.’,  
:l\_rcname =\> ‘chazanga’,  
:l\_raddress1 =\> ‘Suites 17 & 18’,  
:l\_raddress2 =\> ‘9-12 Middle Street’,  
:l\_rcity =\> ‘Brighton’,  
:l\_rpostcode =\> ‘BN1 3TN’,  
:l\_rcountry =\> ‘3’,  
:l\_email =\> ‘removed\_email\_address@domain.invalid’,  
:l\_fb\_emails =\> ‘1’,  
:p\_user\_id =\> ‘2054’  
}

response = Net::HTTP.start(host, port) do |http|  
get = Net::HTTP::Get.new path  
get.form\_data = param\_hash  
get.content\_type = “application/x-www-form-urlencoded”  
http.request get  
end
