【问题标题】:Making a HTTPoison POST multipart request with more data other than the file使用文件以外的更多数据发出 HTTPoison POST 多部分请求
【发布时间】:2017-03-20 02:22:34
【问题描述】:

我正在尝试构建一个函数,以使用 this 作为指南,通过多部分格式的 POST 请求发送文件,但无论我做出什么更改,HTTPoison 都会不断给我两个错误到表格。他们都是

HTTPoison.post("https://api.telegram.org/myCredentials", {:multipart, form}, headers)

我的表单的三个版本和错误如下(无论我是否使用标题):

第一版和第二版(两者的错误相同):

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 及其资源,在经历了几次类似的事故后最终学会了它。

【问题讨论】:

    标签: http elixir multipart httpoison


    【解决方案1】:

    查看HTTPoison.post/3 的文档。第二个参数是 body 类型,如果您查看该类型规范的文档,您会发现:

    body :: binary | {:form, [{atom, any}]} | {:file, binary}
    

    您的第二个参数与该类型规范不匹配。您正在发送{: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}]

    【讨论】:

    • 您所说的表单的问题是 Telegram api 要求文件在“照片”对象中发送。比如{chat_id:destination, photo: multipart/form-data file, 其他参数修改发送到api的消息}。您对此有何建议?
    • 我才想起来,我已经能够发送这样一个请求:HTTPoison.post! url, {:form, [some: "data"]},已经被HTTPbin成功接收为表单
    • 如您所见here,代码发出的任何请求都不能有两个或多个{:body, content}{:form, content} 等参数,因为第三个参数将作为标头处理。编辑:但是,那里似乎根本没有提到多部分。可能性是原始二进制、char 列表或 iotlist 或元组 :form:file:stream
    猜你喜欢
    • 2020-06-18
    • 2016-11-14
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 2011-05-06
    • 2016-10-31
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多