Using inets http client
Michael McDaniel
erlang@REDACTED
Sun Jun 5 06:48:12 CEST 2005
On Sun, Jun 05, 2005 at 03:24:03AM +0100, Will Newton wrote:
> On Saturday 04 June 2005 20:37, Michael McDaniel wrote:
>
> Thanks for your quick reply.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You are welcome.
~M
>
> > It appears that when the http:request/4 returns (successfully),
> > http_wait_with_timeout(RequestId, 15000) is then called and prints dots.
> >
> > I suggest adding
> > io:format'~p~n', [RequestId]) ,
> >
> > just before the call to http_wait_with_timeout(RequestId, 15000) in
> > get_url/1 and see what gets printed.
>
> #Ref<0.0.0.68>
>
> I'm not sure how to interpret this value...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Apologies for any confusion I caused. I am accustomed to using
http:request/4 synchronously and directly and using the URL
response returned.
When I wrote the first note I had not run the code you provided.
>From looking at it, I was thinking that 'RequestId' would actually
contain the result from the request. Of course it does not.
Now I also am confused about why this is not working.
Interestingly, this
get_url(Url) ->
application:start(inets),
case (catch http:request(get, {Url, []}, [], [{sync, false}])) of
{ok, RequestId} ->
receive
{http, {RequestId, Result}} ->
io:format('~p~n', [Result]) ;
{http, {RequestId, {error, Reason}}} ->
throw({error, Reason})
after 7000 -> throw({timeout})
end ;
_ ->
io:format("http request failed~n")
end.
will return the Result.
According to documentation of http:request/4, "... a message will be sent to the
calling process on the format ...".
Both get_url/1 and http_wait_with_timeout/2 have the same process id so I am
not understanding why the result does not come back to http_wait_with_timeout/2
but does come back to get_url/1.
The following will retry also.
get_url(Url, Timeout) ->
application:start(inets),
if Timeout < 0 -> exit({timeout}) ;
true -> true
end ,
case (catch http:request(get, {Url, []}, [], [])) of
{ok, Result} -> {ok, Result} ;
_ ->
io:format('.', []) , timer:sleep(1000) ,
get_url(Url, Timeout-1000)
end.
Any help from others about the original code is welcome.
~Michael
More information about the erlang-questions
mailing list