[erlang-questions] Sometime it's the simple stuff...

Ingela Anderton Andin ingela@REDACTED
Mon May 5 09:12:57 CEST 2008


> I need to take an IP address in the form of "[{10,0,1,100}]" and  
> create an http URL like this:  "http://10.0.1.100/".  I've tried using  
> io_lib:format and successfully generated a list.  But, the inets http  
> client  doesn't  like the format and complains of a malformed URL.

Here I use io_lib:format to create a URL-string from your exampel (I guess that you mean that you have the ip-address on tupel
format and not that you have a string looking like "[{10,0,1,100}]") and
the inets client will parse it just fine. I did not do http:request as I have
no server at this address...

1> URL = lists:flatten(io_lib:format("http://~w.~w.~w.~w/", tuple_to_list({10,0,1,100}))).
"http://10.0.1.100/"
2> http_uri:parse(URL).
{http,[],"10.0.1.100",80,"/",[]}

... but to prove my point I can do:

3> http:request(URL).
{error,econnrefused}

... e.i it tried to call gen_tcp:connect and got connection refused, hence it has already parsed the URL
successfully.

But if you miss out one of the first "/" for instance:

4> http_uri:parse("http:/10.0.1.100/").
{error,{malformed_url,"http:/10.0.1.100"}}

5> http:request("http:/10.0.1.100/").
{error,{malformed_url,"http:/10.0.1.100"}}

Maybe you missed something?

Regards Ingela -OTP team




More information about the erlang-questions mailing list