title: Working with a compressed base64 query parameter with NodeJS
body: <p>I'm writing a Node based web service that is going expecting a third party to send a SAML request that's deflated and base64 encoded. After flailing around for the better part of the day I fear I'm not sure I even know what to ask (I apologize if this is a dupe).</p>
  
  <p>The docs say:</p>
  
  <blockquote>
    <p>The SAMLRequest is first DEFLATE-compressed, then Base 64 encoded, then URL encoded. It must be decoded and parsed</p>
  </blockquote>
  
  <p>The Webstorm Console log has this query string:</p>
  
  <blockquote>
    <p>GET /login?SAMLRequest=fZJRa9swEMff9ymE3m1LipM4InbJ0mwrNCM0bqF9Gap8cQW2lOnksH37ObFbWgZ9lLj76X%2F30%2FLqT9uQE3g0zuaUx4wSsNpVxtY5vS%2B%2FRRm9Kr4sUbWNOMpVF17sHfzuAANZIYIPfd%2FaWexa8HvwJ6Ph%2Fu42py8hHFEmCV%2BImM%2BymMWCswRBd96Ev1GrrKrBJ2eueuXokUPJdc83VoVLqDPqA4lPmJwwxpLG1cZScnOd018HJeaLSsNkqhfAqql6nsyyVFQiEweWqnlfhrv%2BKXOCnB5Ug3C%2BwQ5uLAZlQ04F42nE5hHPSj6VKZc8jfmcP1Gy8y447Zqvxg6L6byVTqFBaVULKIOW%2B9X2VoqYyeehCOWPstxFq36yg9LhAjmZCvzPviOn352rGyAbG8AfvUEg%2B3E3ZDvshpKHVy3irKUXZVEOIj5PcBzj0mLwJi9z%2BveEzwFvRmgxLr%2B%2BxI21axN4i5zUqJIyjTaP23SzftyK9Wwm%2FnO8TN6HKMbjx79U%2FAM%3D</p>
  </blockquote>
  
  <p>Output of</p>
  
  <pre><code>console.log(req.query.SAMLRequest)
  </code></pre>
  
  <p>shows the URL decoding is automatically done:</p>
  
  <blockquote>
    <p>fZJRa9swEMff9ymE3m1LipM4InbJ0mwrNCM0bqF9Gap8cQW2lOnksH37ObFbWgZ9lLj76X/30/LqT9uQE3g0zuaUx4wSsNpVxtY5vS+/RRm9Kr4sUbWNOMpVF17sHfzuAANZIYIPfd/aWexa8HvwJ6Ph/u42py8hHFEmCV+ImM+ymMWCswRBd96Ev1GrrKrBJ2eueuXokUPJdc83VoVLqDPqA4lPmJwwxpLG1cZScnOd018HJeaLSsNkqhfAqql6nsyyVFQiEweWqnlfhrv+KXOCnB5Ug3C+wQ5uLAZlQ04F42nE5hHPSj6VKZc8jfmcP1Gy8y447Zqvxg6L6byVTqFBaVULKIOW+9X2VoqYyeehCOWPstxFq36yg9LhAjmZCvzPviOn352rGyAbG8AfvUEg+3E3ZDvshpKHVy3irKUXZVEOIj5PcBzj0mLwJi9z+veEzwFvRmgxLr++xI21axN4i5zUqJIyjTaP23SzftyK9Wwm/nO8TN6HKMbjx79U/AM=</p>
  </blockquote>
  
  <p>So I assume I must decode the base64 then INFLATE it.  (I've tried to inflate and then decode but that still failed and this makes the most sense):</p>
  
  <pre><code>var zlibJS = require('zlibjs');  //I've tried Node's own zlib but that only appears to work with call backs
  ...
  var b = new Buffer(samlReq, 'base64');
  var decoded = b.toString();
  var inflated = zlibJS.inflateSync(decoded); //https://github.com/imaya/zlib.js/blob/master/test/node-test.js
  //I get an Unsupported compression method 
  ...//relooking at the test it appears it wants a buffer so switch the above line for this
  var inflated = zlibJS.inflateSync(b); // same 500 error
  </code></pre>
  
  <p>Looking through the github source this error is thrown when a switch falls through to the default when looking for a compression method however, I don't see where I'm expected to set that.
  I've tried to log like crazy(stripped out for readability) and put each step into base64 decoder (<a href="http://www.motobit.com/util/base64-decoder-encoder.asp" rel="noreferrer">http://www.motobit.com/util/base64-decoder-encoder.asp</a>), url decoder (<a href="http://meyerweb.com/eric/tools/dencoder/" rel="noreferrer">http://meyerweb.com/eric/tools/dencoder/</a>), and a string uncompressor (<a href="http://i-tools.org/gzip" rel="noreferrer">http://i-tools.org/gzip</a>) with inconclusive results (the uncompressor tool fails to uncompress its own compression so...)</p>
  
  <p>Thanks for any advice, tips and or tools that might help</p>