<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Hesham Abdelkader</title>
    <description>The latest articles on DEV Community by Ahmed Hesham Abdelkader (@ahmedhesham6).</description>
    <link>https://dev.to/ahmedhesham6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F600857%2Feb4c4b10-9b3e-47ba-92a7-3d43bbf6d084.jpeg</url>
      <title>DEV Community: Ahmed Hesham Abdelkader</title>
      <link>https://dev.to/ahmedhesham6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedhesham6"/>
    <language>en</language>
    <item>
      <title>Forwarding PDFs with Axios</title>
      <dc:creator>Ahmed Hesham Abdelkader</dc:creator>
      <pubDate>Sun, 06 Jun 2021 18:05:56 +0000</pubDate>
      <link>https://dev.to/ahmedhesham6/forwarding-pdfs-with-axios-nog</link>
      <guid>https://dev.to/ahmedhesham6/forwarding-pdfs-with-axios-nog</guid>
      <description>&lt;p&gt;Trying to integrate with an E-commerce platform for a mobile app by sending PDF invoices through a node express app&lt;/p&gt;

&lt;h2&gt;
  
  
  Axios
&lt;/h2&gt;

&lt;p&gt;So the idea was to fetch the PDF data and return it to the client&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The url is not directly used in the client as the E-commerce app requires &lt;code&gt;api-key&lt;/code&gt; which is only defined in the server app&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require('axios');
const url = "http://www.africau.edu/images/default/sample.pdf"
axios.defaults.headers.common['Authorization'] = "secret-key";

app.get('/download', async function(req, res) {
   try {
      // Fetch PDF from desired url
      const pdf = await axios.get(url);

      // Set response header to pdf 
      res.setHeader('Content-Type', 'application/pdf');

      // Return PDF data to client
      return res.send(pdf.data);
    } catch (err) {

      // Throw if any error occurred
      return res.status(400).json(err);
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Testing this endpoint, a blank PDF document response was returned&lt;/p&gt;

&lt;p&gt;Going through multiple forms and docs, trying different methods to achieve the same end result&lt;/p&gt;

&lt;p&gt;Until I found the answer when I was just giving up where axios have a problem with requesting PDF&lt;/p&gt;

&lt;p&gt;Where &lt;code&gt;{ responseType: 'arraybuffer' }&lt;/code&gt; is provided to axios options api&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      await axios.get(url,{responseType:'arraybuffer'});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>axios</category>
      <category>pdf</category>
    </item>
  </channel>
</rss>
