[PATCH] httpc module - allow streaming of bodies for PUT and POST requests

Filipe David Manana <>
Sun Sep 26 13:04:02 CEST 2010


Hi,

The following patch, as a github commit, allows for streaming of HTTP PUT
and POST requests.

http://github.com/fdmanana/otp/commit/2d0e66d9d41c813b6fb09179bdcd29910bf0d296

Usage example:

-module(httpc_post_stream_test).
-compile(export_all).

-define(LEN, 1024 * 1024).

prepare_data() ->
    {ok, Fd} = file:open("test_data.dat", [binary, write]),
    ok = file:write(Fd, lists:duplicate(?LEN, "1")),
    ok = file:close(Fd).

test() ->
    inets:start(),
    ok = prepare_data(),
    {ok, Fd1} = file:open("test_data.dat", [binary, read]),
    BodyFun = fun(Fd) ->
        case file:read(Fd, 512) of
        eof ->
            eof;
        {ok, Data} ->
            {ok, Data, Fd}
        end
    end,
    {ok, {{_, 200, _}, _, _}} = httpc:request(post, {"http://localhost:8888
",
        [{"content-length", integer_to_list(?LEN)}], "text/plain", {BodyFun,
Fd1}}, [], []),
    ok = file:close(Fd1).


This is specially useful when sending data to RESTful servers, like CouchDB
for example.

Let me know if the patch meets all the necessary requirements for
acceptance.

cheers

-- 
Filipe David Manana,
, 

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."


More information about the erlang-patches mailing list