| Author |
Message |
|
| seancharles |
Posted: Thu Nov 01, 2007 8:39 am |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
What is the *easiest* and *cleanest* (i.e. minimum code) to do a simple HTTP POST to a URl with parameters and get back the response ?
I have looked at the yaws code, ibrowse code but my lack of Erlang experience has left me none the wiser on how to do what I want to do.
I am trying to interface to an SMS service in Erlang, having done it in Coldfusion for the day job I thought I would write a comparison so *they* can know that I was right when I said we should use Erlang.
Coldfusion is a lobotomy one keysroke (plus numerous angle brackets) at a time.
Can anybody shed some light, docs to read, examples etc etc
Thanks
Sean Charles |
|
|
| Back to top |
|
| seancharles |
Posted: Sun Nov 04, 2007 10:03 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
OK, regarding this thread: http://www.trapexit.org/forum/viewtopic.php?p=33038 where it was rumored that ibrowse:send_req could do a POST, the thread hinted at the README in 1.2.9.
I read the README and there is *NO* example of a POST to be found anywhere. So, after sniffing the code and thinking about it I came up with this example:
Code:
-module(itest).
-compile([export_all]).
go()->
ibrowse:start(),
ibrowse:send_req(
"http://?????????.com/win/tests/posttest.cfm",
[{"Content-Type","application/x-www-form-urlencoded"}],
post,
"X=1&y=2&z=3333"
).
It didn't work against a Coldfusion server unless you add the application/x-www.... and the final string should be URL encoded as the server would expect, all of which is standard HTTP expectation!
Code:
Output, run from erlang-shell in EMACS:
itest:go().
{ok,"200",
[{"Date","Sun, 04 Nov 2007 21:54:09 GMT"},
{"Server","Apache/2.0.54 (Linux/SUSE)"},
{"Set-Cookie","CFID=108909;expires=Tue, 27-Oct-2037 21:54:09 GMT;path=/"},
{"Set-Cookie",
"CFTOKEN=62929827;expires=Tue, 27-Oct-2037 21:54:09 GMT;path=/"},
{"Transfer-Encoding","chunked"},
{"Content-Type","text/html; charset=UTF-8"}],
"[ECHO:X = 1]<br/> [ECHO:Y = 2]<br/> [ECHO:Z = 3333]<br/> "}
And for completeness, the little bit of Coldfusion that I posted to:
Code:
<!--- Echo back the post parameters --->
<cfif lcase(cgi.request_method) eq "post">
<cftry>
<cfloop list="#form.fieldnames#" index="f">
<cfoutput>[ECHO:#f# = #form[f]#]<br/></cfoutput>
</cfloop>
<cfcatch type="any">
<cfoutput>:#cfcatch.Message#</cfoutput>
</cfcatch>
</cftry>
<cfelse>
GET was used.
</cfif>
There is plenty of code to do that laying around the place.
Now that I have the ability to POST I will play a little more and see what other styles the Body() part of send_req can process, I tried lists of variables but it bombed on me, that is,
results in the message:
Code: Bad value on output port 'tcp_inet'
{error,send_failed}
Keep on trying....
So, cool, I answered my own question, learned a lot more in the process, which is what it's all about, and I know I like Erlang more and more with each passing day!
(Sad git) We love it! LOL |
|
|
| Back to top |
|
| Kenneth |
Posted: Sun Nov 11, 2007 9:17 pm |
|
|
|
Joined: 28 Feb 2005
Posts: 5
Location: Stockholm, Sweden
|
|
| Back to top |
|
| seancharles |
Posted: Sun Nov 11, 2007 10:18 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
My shiny new copy of Programming Erlang should be arriving tomorrow.
Why oh why oh why oh why did I fail to see this at all!!!
Oh my god!
Should I even continue.....
Thanks for that.
Sean.
 |
|
|
| Back to top |
|
| seancharles |
Posted: Mon Nov 12, 2007 7:25 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
An example of a POST using the standard 'http' request method seemed to be in order after my appallingly bad job of reading the documentation!
Code:
-module(echo).
-export([pt/0]).
pt() ->
http:request(
post,
{
"http://scream-it.com/win/tests/posttest.cfm",
[],
"application/x-www-form-urlencoded",
"x=2&y=3&msg=Hello%20World"
},
[],
[]
).
This does the same as my other example using the ibrowse module, producing the following output from the same little Coldfusion script:
Code:
{ok,{{"HTTP/1.1",200,"OK"},
[{"connection","Keep-Alive"},
{"date","Mon, 12 Nov 2007 19:08:52 GMT"},
{"server","Apache/2.0.54 (Linux/SUSE)"},
{"content-length","66"},
{"content-type","text/html; charset=UTF-8"},
{"set-cookie",
"CFID=141882;expires=Wed, 04-Nov-2037 19:08:52 GMT;path=/"},
{"set-cookie",
"CFTOKEN=19286971;expires=Wed, 04-Nov-2037 19:08:52 GMT;path=/"},
{"keep-alive","timeout=15, max=100"}],
"[ECHO:X = 2]<br/> [ECHO:Y = 3]<br/> [ECHO:MSG = Hello World]<br/> "}}
I hope my Joe book turns up tomorrow!
Thanks everybody.
Sean Charles. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|