mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Fix upload example
This commit is contained in:
parent
44f16f3b1e
commit
57901a7116
2 changed files with 32 additions and 4 deletions
|
@ -13,12 +13,12 @@ start(_Type, _Args) ->
|
||||||
Dispatch = cowboy_router:compile([
|
Dispatch = cowboy_router:compile([
|
||||||
{'_', [
|
{'_', [
|
||||||
{"/", cowboy_static, {priv_file, upload, "index.html"}},
|
{"/", cowboy_static, {priv_file, upload, "index.html"}},
|
||||||
{"/upload", upload_handler, []},
|
{"/upload", upload_handler, []}
|
||||||
{"/files/[...]", cowboy_static, {priv_dir, upload, "files"}}
|
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
|
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
|
||||||
[{env, [{dispatch, Dispatch}]}]),
|
env => #{dispatch => Dispatch}
|
||||||
|
}),
|
||||||
upload_sup:start_link().
|
upload_sup:start_link().
|
||||||
|
|
||||||
stop(_State) ->
|
stop(_State) ->
|
||||||
|
|
|
@ -348,6 +348,34 @@ do_markdown_middleware(Transport, Protocol, Config) ->
|
||||||
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
%% Upload.
|
||||||
|
|
||||||
|
upload(Config) ->
|
||||||
|
doc("Upload example."),
|
||||||
|
try
|
||||||
|
do_compile_and_start(upload),
|
||||||
|
do_upload(tcp, http, Config),
|
||||||
|
do_upload(tcp, http2, Config)
|
||||||
|
after
|
||||||
|
do_stop(upload)
|
||||||
|
end.
|
||||||
|
|
||||||
|
do_upload(Transport, Protocol, Config) ->
|
||||||
|
{200, _, << "<html>", _/bits >>} = do_get(Transport, Protocol, "/", Config),
|
||||||
|
%% Use POST to upload a file using multipart.
|
||||||
|
ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
|
||||||
|
Ref = gun:post(ConnPid, "/upload", [
|
||||||
|
{<<"content-type">>, <<"multipart/form-data;boundary=deadbeef">>}
|
||||||
|
], <<
|
||||||
|
"--deadbeef\r\n"
|
||||||
|
"Content-Disposition: form-data; name=\"inputfile\"; filename=\"test.txt\"\r\n"
|
||||||
|
"Content-Type: text/plain\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"Cowboy upload example!\r\n"
|
||||||
|
"--deadbeef--">>),
|
||||||
|
{response, fin, 204, _} = gun:await(ConnPid, Ref),
|
||||||
|
ok.
|
||||||
|
|
||||||
%% Websocket.
|
%% Websocket.
|
||||||
|
|
||||||
websocket(_) ->
|
websocket(_) ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue