How to upload an attachment to a CouchDB document using ibrowse?

I used curl to download the image file Penguins.jpg. For instance: C:\curl>curl -vX PUT -H "Content-Type: image/jpeg" http://localhost:5984/DBNAME/DOCID/Penguins?rev=LATEST_REVISION --data-binary @Penguins.jpg

and he worked ...

So how can I achieve the same using ibrowse? =================================

+5
source share
1 answer

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-, !

+3

All Articles