Packages
httpoison
1.3.0
3.0.0
2.3.0
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.0
1.8.2
1.8.1
1.8.0
1.7.0
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.0
0.13.0
0.12.0
0.11.2
0.11.1
0.11.0
0.10.0
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.0
Yet Another HTTP client for Elixir powered by hackney
Current section
Files
Jump to
Current section
Files
lib/httpoison/handlers/multipart.ex
defmodule HTTPoison.Handlers.Multipart do
@moduledoc """
Provides a set of functions to handle multipart requests/responses
`HTTPoison.Handlers.Multipart` defines the following list of functions:
# Used to parse a multipart response body
# @type body :: binary | {:form, [{atom, any}]} | {:file, binary}
# @spec decode_body(Response.t()) :: body
# def decode_body(response)
"""
alias HTTPoison.Response
@callback decode_body(Response.t()) :: body
@type body :: binary | {:form, [{atom, any}]} | {:file, binary}
@doc """
Parses a multipart response body.
It uses `:hackney_headers` to understand if the content type of the response
is multipart, in which case it uses `:hackney_multipart` to decode the body of
the response.
For example, if we have the following `multipart` response body:
--123
Content-type: application/json
{\"1\": \"first\"}
--123
Content-type: application/json
{\"2\": \"second\"}
--123--
We can parse the body of the response to its various parts:
HTTPoison.Handlers.Multipart.decode_body(response)
#=> will decode a multipart body, e.g. yielding
# [
# {[{"Content-Type", "application/json"}], "{\"1\": \"first\"}"},
# {[{"Content-Type", "application/json"}], "{\"2\": \"second\"}"}
# ]
In case the content type is not multipart, the original body is returned.
"""
def decode_body(%Response{body: body, headers: headers}) do
try do
case :hackney_headers.parse("Content-Type", headers) do
{"multipart", _, [{"boundary", boundary} | _]} ->
case :hackney_multipart.decode_form(boundary, body) do
{:ok, []} -> body
{:ok, parsed} -> parsed
{_, _} -> body
end
_ ->
body
end
rescue
_ in ErlangError -> body
end
end
end