尝试使用curl上传文件
curl -v -F 'file1=@/home/user/test/aaa/validation_utils.erl' -F 'file2=@/home/user/test/aaa/README.md' http://localhost:8083/api/list
但只接收一个(我可以从日志中看到)
[warning] Form_Data:
[{<<"content-type">>,<<"application/octet-stream">>},{<<"content-disposition">>,<<"form-data; name=\"file1\"; filename=\"validation_utils.erl\"">>}]
这是我的get_body函数:
get_body(<<"POST">>, AllHeaders, Req, _MaxFileSize) ->
case maps:get(<<"content-type">>, AllHeaders) of
<<"application/json", _Rest/binary>> ->
case catch cowboy_req:read_body(Req) of
{ok, <<>>, Req2} ->
{ok, [], Req2};
{ok, JsonBody, Req2} when JsonBody =/= <<>> ->
try
{ok, ?JSON_DECODE(JsonBody), Req2}
catch
_:Err ->
Err
end
end;
<<"multipart/form-data", _Rest/binary>> ->
{ok, Headers, Req2} = cowboy_req:read_part(Req),
{ok, Data, _Req3} = cowboy_req:read_part_body(Req2),
{file, _, Filename, _ContentType, _BitSize} = cow_multipart:form_data(Headers),
Body = [{<<"filename">>, Filename}, {<<"base64_file">>, base64:encode(Data)} | cowboy_req:parse_qs(Req)],
{ok, Body, Req};
_ ->
cowboy_req:read_urlencoded_body(Req)
end;
答案 0 :(得分:1)
知道了。
我需要迭代cowboy_req:read_part(Req)
multipart(Req0) ->
case cowboy_req:read_part(Req0) of
{ok, Headers, Req1} ->
?LOG_WARNING("Headers: p~n", [Headers]),
{ok, _Body, Req} = cowboy_req:read_part_body(Req1),
multipart(Req);
{done, Req} ->
?LOG_WARNING("Req: p~n", [Req]),
Req
end.
如此处所述:https://ninenines.eu/docs/en/cowboy/2.0/guide/multipart/