1

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.

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.

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 using a payload body.

For example:

  • Is using stream.write the recommended way to send DATA frames, or should I be using a built-in function provided by http2?
  • Do I pass arguments in plaintext, and the http2 protocol takes care of the binary encoding, or do I encode it myself?
  • How should I modify below code to send a payload body?

.

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) => {
  console.log('RESPONSE', headers)
  stream.on('data', (data) => console.log('DATA', data))
  stream.on('end', () => console.log('END'))
})

stream.write(Buffer.from('POST-request-payload-body-here?'))
  • Possible duplicate of [Sending HTTP/2 POST request NodeJS](https://stackoverflow.com/questions/48491814/sending-http-2-post-request-nodejs) – Barry Pollard Aug 27 '18 at 08:58
  • @BarryPollard the question in that thread is similar, but doesn't explain how to keep the connection open; sending .write() followed by .end() immediately closes the connection, whereas here I am looking to keep the connection persistently open –  Aug 27 '18 at 09:01
  • The stream should be closed but have you tried to just make another request on the same connection? Note most servers will timeout and close the whole connection if another requests is not received within a set amount of time. – Barry Pollard Aug 27 '18 at 09:07

2 Answers2

2
  • The first thing that you need to do is the to convert the body data into a buffer

    var buffer = new Buffer(JSON.stringify(body));

  • Them you need to update the connection.request object with Content-Type and Content-Length keys. Note the Content-Length is the length of buffer
   const stream = connection.request({
             ':authority':'www.example.com',
             ':scheme':'https',
             ':method': 'POST',
             ':path': '/custom/path',
             'Content-Type': 'application/json',
             'Content-Length': buffer.length
   }, { endStream: false })

  • Finally you need to send the request by Converting the body data into a string

   stream.end(JSON.stringify(body));

selftaught91
  • 7,013
  • 3
  • 20
  • 26
0

It work in my project

const body ={any}
const req = client.request({ 
        ':path': '/',
        ':method':'POST',
        'Content-Type': 'application/json',///set body
    });
                
    req.write(JSON.stringify(body), 'utf8');///set body