0

How can send an http/2 post request in Node.js? I want to send the following request.

curl --http2 "POST" "http://hostname:8088/query-stream" -d $'{"sql": "SELECT * FROM `USERPROFILE` EMIT CHANGES;", "properties": {"ksql.streams.auto.offset.reset": "earliest" } }'
Armen Chakhalyan
  • 349
  • 1
  • 2
  • 4

1 Answers1

0

The package fetch-h2 has http/2 support:

import { fetch } from 'fetch-h2'

const url = 'https://hostname:8088/query-stream'
const method = 'POST';
const json = {"sql": "SELECT * FROM `USERPROFILE` EMIT CHANGES;", "properties": {"ksql.streams.auto.offset.reset": "earliest" } };
 
const response = await fetch( url, { method, json } );

Note that by default fetch-h2 uses http/1.1 for http:// addresses. Either change the URL or configure fetch-h2 to explicitly use http/2 even for http:// requests.

torusJKL
  • 184
  • 1
  • 4
  • thanks for answering but I think fetch sending http/1.1 request. not support http/2 the response is this in that example { '@type': 'generic_error', error_code: 40004, message: 'This endpoint is only available when using HTTP2' } ➜ – Armen Chakhalyan Jun 01 '21 at 12:32
  • You are correct. I wasn't aware of that. I changed my answer with a solution that should support http/2 – torusJKL Jun 02 '21 at 03:25
  • Thank You. I have used also fetch-h2 it also problematic for that request. for that request will open the connection and if any changes will send an event, like a socket. response to that example is following` Client network socket disconnected before secure TLS connection was established – Armen Chakhalyan Jun 02 '21 at 07:27