title: How to make HTTP/2 request with body payload?
body: <p>I am looking to open a HTTP/2 stream and use that stream to make multiple HTTP/2 POST requests. Each POST request will have its own body payload.</p>
  
  <p>I currently have the code below, which works for requests that don't require a payload, but I'm not sure how to customize it for requests that do require a payload.</p>
  
  <p>I've read RFC 7540 and nearly every related post on SO, but I'm still finding it difficult to write working code of HTTP/2 <em>using a payload body</em>.</p>
  
  <p>For example:</p>
  
  <ul>
  <li>Is using stream.write the recommended way to send DATA frames, or should I be using a built-in function provided by http2?</li>
  <li>Do I pass arguments in plaintext, and the http2 protocol takes care of the binary encoding, or do I encode it myself?</li>
  <li>How should I modify below code to send a payload body?</li>
  </ul>
  
  <p>.</p>
  
  <pre><code>const http2 = require('http2')
  const connection = http2.connect('https://www.example.com:443')
  
  const stream = connection.request({
    ':authority':'www.example.com',
    ':scheme':'https',
    ':method': 'POST',
    ':path': '/custom/path',
  }, { endStream: false })
  
  stream.setEncoding('utf8')
  
  stream.on('response', (headers) =&gt; {
    console.log('RESPONSE', headers)
    stream.on('data', (data) =&gt; console.log('DATA', data))
    stream.on('end', () =&gt; console.log('END'))
  })
  
  stream.write(Buffer.from('POST-request-payload-body-here?'))
  </code></pre>