To make a http request with a custom body as binary and return a binary response in Typescript, you can use the http
module and Buffer
class that come with Node.js.
Here's an example of how to do it:
index.ts911 chars37 lines
In this example, we're creating a new http
request using the http.request()
method. We specify the request method, hostname, path, and headers. In this case, we're telling the server that we're sending binary data by setting the Content-Type
header to application/octet-stream
and setting the Content-Length
header to the length of the binary data.
We then set up event listeners to accumulate the response data as it comes in. Once the response is complete, we join all the response data chunks into a single Buffer
using Buffer.concat()
. We can then do something with the response body.
Finally, we send the binary data in the request body using the req.write()
method and end the request using req.end()
.
gistlibby LogSnag