amal-sultan

amal-sultan

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.

Most Liked

shamshirz

shamshirz

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

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement