Naturally, downloading a file is this HTTP POST . Now let's first write the Erlang code snippet that does HTTP/1.1 POST with Ibrowse .
%% Assumes Ibrowse application is in Code path
ensure_ibrowse () ->
case whereis (ibrowse) of
undefined -> ibrowse: start ();
_ -> ok
end.
post (Link, Data, Headers) ->
ensure_ibrowse (),
try ibrowse: send_req (Link, Headers, post, Data) of
{ _, _, _,Result} ->
io:format("\n\tFile Uploaded. Return: ~p~n",[Result]);
EE -> {error,EE}
catch
XX:XX2 -> {error,XX,XX2}
end.
, Couch DB.
-define(Link,"http://localhost:5984/DBNAME/DOCID/Penguins?rev=LATEST_REVISION").
%% File_path must be a valid file !
upload_file(Full_file_path)->
case file:read_file(Full_file_path) of
{ok,Binary} ->
post(?Link,Binary,[{"Content-Type","image/jpeg"}]);
Error -> Error
end.
! , , Link, DB-, !