使用hackney流式传输文件

时间:2016-11-15 17:53:20

标签: erlang streaming elixir multipart

我正在尝试使用hackney来传输多部分表单。

这是我目前的代码:

method = :post
path = "https://httpbin.org/post"
req_headers = [
  {"Content-Type", "multipart/form-data"}
]

{:ok, pid} = :hackney.request(method, path, req_headers, :stream, [])

:hackney.send_multipart_body(pid, {:file, "/Users/gmile/.vimrc"})

{:ok, _status, _headers, pid} = :hackney.start_response(pid)

{:ok, body} = :hackney.body(pid)

:hackney.send_multipart_body/2一致,我看到异常:

** (ArgumentError) argument error
    :erlang.byte_size(nil)
    (hackney) src/hackney_multipart.erl:134: :hackney_multipart.mp_header/2
    (hackney) src/hackney_multipart.erl:239: :hackney_multipart.mp_file_header/2
    (hackney) src/hackney_request.erl:222: :hackney_request.stream_multipart/2
    (hackney) src/hackney.erl:432: anonymous fn/2 in :hackney.send_multipart_body/2
    read_and_stream.exs:13: (file)

问题:我做错了什么?

我的代码中明显存在一个错误,但由于关于:hackney.send_multipart_body/2的文档很少,我无法解决这个问题。

我开始调试了。

以下是来自堆栈跟踪的执行路径,在调用顺序中:

  1. https://github.com/benoitc/hackney/blob/master/src/hackney.erl#L432
  2. https://github.com/benoitc/hackney/blob/master/src/hackney_request.erl#L222
  3. https://github.com/benoitc/hackney/blob/master/src/hackney_multipart.erl#L239
  4. https://github.com/benoitc/hackney/blob/master/src/hackney_multipart.erl#L134
  5. 如果我理解正确,HTTP客户端进程中应该设置Boundary变量。但是我似乎无法理解它的设置位置。

1 个答案:

答案 0 :(得分:1)

要使用:hackney.send_multipart_body/2,您的请求的正文(第四个参数)应为:stream_multipart,而不是:stream

{:ok, pid} = :hackney.request(method, path, req_headers, :stream_multipart, [])