我正在尝试构建一个函数,以多部分格式通过POST请求发送文件,使用 this 作为指导,但无论发生什么变化,HTTPoison都会给我两个错误我做了表格。他们都是
HTTPoison.post("https://api.telegram.org/myCredentials", {:multipart, form}, headers)
我的表单的三个版本和错误如下(无论我是否使用标题):
第1版和第2版(两者都有相同的错误):
form = [{"photo", [{"name", "myphoto.jpg"}, {:file, "files/aphoto.jpg"}]}, {"chat_id", 237799110}]
-----
form = [photo: [{"name", "myphoto.jpg"}, {:file, "files/aphoto.jpg"}], chat_id: 237799110]
哪个给我这个错误:
** (FunctionClauseError) no function clause matching in anonymous fn/2 in :hackney_multipart.len_mp_stream/2
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_multipart.erl:159: anonymous fn({"photo", [{"name", "myphoto.jpg"}, {:file, "files/aphoto.jpg"}]}, 0) in :hackney_multipart.len_mp_stream/2
(stdlib) lists.erl:1263: :lists.foldl/3
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_multipart.erl:159: :hackney_multipart.len_mp_stream/2
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_request.erl:319: :hackney_request.handle_body/4
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_request.erl:81: :hackney_request.perform/2
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney.erl:373: :hackney.send_request/2
(httpoison) lib/httpoison/base.ex:432: HTTPoison.Base.request/9
第三个版本:
form = [chat_id: 237799110, photo: [{"name", "myphoto.jpg"}, {:file, "files/aphoto.jpg"}]]
这给了我以下错误:
** (ArgumentError) argument error
:erlang.byte_size(:chat_id)
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_multipart.erl:255: :hackney_multipart.mp_data_header/2
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_multipart.erl:180: anonymous fn/3 in :hackney_multipart.len_mp_stream/2
(stdlib) lists.erl:1263: :lists.foldl/3
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_multipart.erl:159: :hackney_multipart.len_mp_stream/2
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_request.erl:319: :hackney_request.handle_body/4
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney_request.erl:81: :hackney_request.perform/2
(hackney) c:/Users/venta/projects/elixir/wrapper/deps/hackney/src/hackney.erl:373: :hackney.send_request/2
我发现在一系列事件中发生了不可避免的不幸,这些事件对多部分POST请求施加了这样的障碍,因此我想听取有关导致这些障碍的可能原因的意见。
现在,我非常乐意按照this格式从头开始编写我自己的请求,但我强迫自己使用Elixir及其资源,最终在这样的一些事故后学习它。
答案 0 :(得分:0)
查看HTTPoison.post/3
的文档。第二个参数是body
类型,如果您查看该类型规范的文档,您会发现:
body :: binary | {:form, [{atom, any}]} | {:file, binary}
你的第二个论点与那个typespec不匹配。您正在发送{:multipart, form}
。
HTTPoison上的这个Github Issue似乎确实建议您使用{:multipart, []}
即使它与文档规范不匹配,也可以使用an example test case for multipart is here。
根据您的示例,可能是:
form = [{:file, "files/aphoto.jpg"}, {"name", "myphoto.jpg"}, {"chat_id", 237799110}]