title: Only Receiving Part of Apple Subscription Notification in Google Cloud Function
body: <p>I am trying to set up a Google Cloud Function (GCF) to handle Subscription Notifications from Apple.  I am familiar with GCF, but not very familiar with writing my own REST API and the Nodejs methods of handling the data Apple sends with the notification.  I am receiving the Apple notification, but only a "chunk" of it.  Here's my code (using express and body-parser frameworks).  I put my whole function here to help people since there is absolutely nothing about how to use GCF for Subscription Notifications anywhere I could find on the web (note this code is very much a work in progress and I am new to Nodejs):</p>
  
  <pre><code>// Set up express object
  const express = require('express');
  const app = express();
  const bodyParser = require('body-parser');
  
  exports.iosNotification = functions.https.onRequest((req, res) =&gt; {
  console.log("We are receiving a request from Apple.");
  app.use(bodyParser.json());
  let receipt = req.body.latest_receipt;
  
  console.log(req.body);
  
  const chunks = [];
  req.on('data', chunk =&gt; {
      chunks.push(chunk);
      console.log('A chunk of data has arrived:', chunk);
  });
  req.on('end', () =&gt; {
      const data = Buffer.concat(chunks);
      console.log('Data: ', data);
      console.log('No more data');
  });
  
  const type = req.body.notification_type;
  console.log("Notification type: ", type);
  const lri = req.body.latest_receipt_info;
  console.log(lri, receipt);
  // Verify the receipt.
  validateAppleReceipt(receipt)
          .then((appleResponse) =&gt; {
              console.log("Receipt from App Store server validated.", appleResponse);
                  res.sendStatus(200);
              const oTxId = appleResponse.latest_receipt_info[0].original_transaction_id;
              // Receipt is valid and we let Apple know.  Let's process the notification.
              switch (type) {
                  case 'CANCEL':
                      // User canceled the subscription (like on accidental purchase).
                      console.log("User canceled a subscription.");
  
                      break;
                  case 'DID_CHANGE_RENEWAL_PREF':
                      console.log("The subscriber downgraded.  Effective on next renewal.  Handle.");
  
                      break;
                  case 'DID_CHANGE_RENEWAL_STATUS':
                      console.log("The subscriber downgraded or upgraded.  Effective on next renewal.  Handle.");
  
                      break;
                  case 'DID_FAIL_TO_RENEW':
                      console.log("Subscription has a billing issue.  Check if in billing retry period.");
  
                      break;
                  case 'DID_RECOVER':
                      console.log("Renewal of expired subscription that failed to renew.");
  
                      break;
                  case 'INITIAL_BUY':
                      console.log("Initial purchase.  Ignored because we already handled with another function.");
                      break;
                  case 'INTERACTIVE_RENEWAL':
                      console.log("Interactive renewal.  Not sure if we'll ever see this.");
                      break;
                  case 'RENEWAL':
                      console.log("Renewal after failure.  Same as DID_RECOVER.  Handle there.");
                      break;
                  default:
                      console.log("Hit default.");
                      break;
              };
          })
          .catch((error) =&gt; {
              console.log("Error validating receipt from App Store server.", error);      
          });
      });
  </code></pre>
  
  <p>This is the output I'm getting (which is only a portion of the notification Apple says it is sending).  I don't get <code>notification_type</code> or any of the other parts of the JSON file the Apple docs say I should be receiving:</p>
  
  <pre><code>{ latest_receipt: 'ewoJInNpZ25hdHVyZSIgPSAiQTNVM0FjaDJpbXRPMG53cEtrQW9 &lt;&lt;shortened for this post&gt;&gt;  
  </code></pre>
  
  <p>I never see the console.log for any chunks.</p>
  
  <p>What can I do to make sure I receive all the "chunks" and put them together into the complete JSON file that Apple is sending me so I can work with it?</p>