-
Hi Community, I was trying to do a request that streams content to the server and the this is the example code, thank you ! import sget from 'simple-get'
import stream from 'stream'
import uWS from 'uWebSockets.js'
const port = 9001
const app = uWS.App().post('/*', (res, req) => {
console.log('Posted to ' + req.getUrl())
res.onData((chunk, isLast) => {
/* Buffer this anywhere you want to */
console.log('Got chunk of data with length ' + chunk.byteLength + ', isLast: ' + isLast)
/* We respond when we are done */
if (isLast) {
res.end('Thanks for the data!')
}
})
res.onAborted(() => {
/* Request was prematurely aborted, stop reading */
console.log('Eh, okay. Thanks for nothing!')
})
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port)
let chunk = Buffer.alloc(1024)
const largeStream = new stream.Readable({
read () {
this.push(chunk)
chunk = null
}
})
sget({
method: 'POST',
url: 'http://localhost:9001',
body: largeStream
}, (err, response, body) => {
console.log(err)
console.log(response.statusCode)
})
} else {
console.log('Failed to listen to port ' + port)
}
}) the results get:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
That request is probably chunked. The server does not support it. |
Beta Was this translation helpful? Give feedback.
-
You are Posting data with @uNetworkingAB beat me to the answer :P |
Beta Was this translation helpful? Give feedback.
-
Thank you everyone for the quick response 💯 |
Beta Was this translation helpful? Give feedback.
You are Posting data with
transfer-encoding: chunked
by using that read stream which is not supported but also not necessary way to post data, you can use standard Post see #669 (comment)@uNetworkingAB beat me to the answer :P