title: axios transformRequest - how to alter JSON payload
body: <p>I am using <a href="https://github.com/axios/axios" rel="noreferrer">axios</a> in my Express API and I want to transform the payload before sending it off to another API. axios has just the thing for this called <a href="https://github.com/axios/axios#request-config" rel="noreferrer"><code>transformRequest</code></a>. This is where I ran into issues though. </p>
  
  <p>The code I have looks like:</p>
  
  <pre><code>const instance = axios.create({
    baseURL: 'api-url.com',
    transformRequest: [
      (data, headers) =&gt; {
        const encryptedString = encryptPayload(JSON.stringify(data));
  
        data = {
          SecretStuff: encryptedString,
        };
  
        return data;
      },
    ],  
  });
  
  // firing off my request using the instance above:
  const postData = {
      id: 1,
      name: 'James',
  };
  instance.post('/getStuff', postData)
  </code></pre>
  
  <p>and ultimately, I want to post <code>api-url.com</code> the JSON: <code>{"SecretStuff": "some-base64-string"}</code> - not the <code>postData</code> object shown above.</p>
  
  <p>From the docs, it says: "The last function in the array must return a string or an instance of Buffer, ArrayBuffer, FormData or Stream" - but of course here I am returning an object, data. Oddly enough in the axios docs it shows them returning <code>data</code> from <code>transformRequest</code>, but in their case that must be the correct data type.</p>
  
  <p>How do I <em>actually</em> transform a payload with axios?</p>