oldhomemovie
oldhomemovie

Reputation: 15129

Streaming a file using hackney

I'm trying to stream a multipart form using hackney.

This is my current code:

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)

On line with :hackney.send_multipart_body/2, I see get exception:

** (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)

Question: what am I doing wrong?

There's clearly a bug in my code, but since there's little documentation about :hackney.send_multipart_body/2 I can't wrap my head around this.


I started debugging.

Here's the execution path from the stacktrace, in the call order:

  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

If I understand correctly, there should a Boundary variable set in the HTTP client process. However I can't seem to understand where it is being set.

Upvotes: 0

Views: 1107

Answers (1)

Dogbert
Dogbert

Reputation: 222118

In order to use :hackney.send_multipart_body/2, your request's body (fourth argument) should be :stream_multipart, not :stream:

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

Upvotes: 0

Related Questions