Handling Multipart response of adobe pdf api in elixir

Hi Everyone, I am using Adobe pdf api to convert html into pdf. I tried the api on postman and it responds with multipart response,(I saved the response through “save response” option in postman and the document is as expected). Now the issue is that I am trying to convert the response into pdf document in elixir code and I am using HTTPoison.Handlers.Multipart to get the document part from the response so that I can save it locally. But I am unable to get the data in the document. The document I get is blank. Can someone please guide me about multipart response handling in elixir. Also it would be nice to connect with someone who has used Adobe pdf api.

Hey Amal, working with the Adobe Document Status API I was able to handle their response like this!

Via Postman, the response was multipart with the body of the response in 1 JSON and 1 octet-stream.
(header Content-Type: multipart/mixed; boundary=Boundary_583415_1634289954_1663190050427;charset=UTF-8)

--Boundary_583415_1634289954_1663190050427
Content-Type: application/json
Content-Disposition: form-data; name="contentAnalyzerResponse"

{"cpf:inputs": … :{"documentOut":{"cpf:location":"multipartLabel","dc:format":"application/pdf"}}}
--Boundary_583415_1634289954_1663190050427
Content-Type: application/octet-stream
Content-Disposition: form-data; name="multipartLabel"

%PDF-1.7
…
%%EOF

--Boundary_583415_1634289954_1663190050427--

HTTPoison ships a function to parse multipart bodies HTTPoison.Handlers.Multipart.decode_body(response). From there we can parse the file body and then write that to disk to read it!
Hex Doc: decode_body/1

with {:ok, %{status_code: code} = response} when code in [200, 202] <- HTTPoison.get(url, headers),
     {:decode, parts} when is_list(parts) <- {:decode, HTTPoison.Handlers.Multipart.decode_body(response)},
     {_headers, file_body} <- Enum.find(parts, fn {headers, _body} -> headers |> Map.new() |> Map.get("Content-Type") == "application/octet-stream" end) do
  File.write!("./test.pdf", file_body)
end
1 Like