Code files
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const http = require('http')
|
||||
const assert = require('assert')
|
||||
const url = 'http://www.davidmarkclements.com/ncb3/some.json'
|
||||
|
||||
http.get(url, (res) => {
|
||||
const size = parseInt(res.headers['content-length'], 10)
|
||||
const buffer = Buffer.allocUnsafe(size)
|
||||
var index = 0
|
||||
res.on('data', (chunk) => {
|
||||
chunk.copy(buffer, index)
|
||||
index += chunk.length
|
||||
})
|
||||
res.on('end', () => {
|
||||
assert.equal(size, buffer.length)
|
||||
console.log('GUID:', JSON.parse(buffer).guid)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
const http = require('http')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const steed = require('steed')()
|
||||
const files = process.argv.slice(2)
|
||||
const boundary = Date.now()
|
||||
const opts = {
|
||||
method: 'POST',
|
||||
hostname: 'localhost',
|
||||
port: 8080,
|
||||
path: '/',
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data; boundary="' + boundary + '"',
|
||||
'Transfer-Encoding': 'chunked'
|
||||
}
|
||||
}
|
||||
|
||||
const req = http.request(opts, (res) => {
|
||||
console.log('\n Status: ' + res.statusCode)
|
||||
process.stdout.write(' Body: ')
|
||||
res.pipe(process.stdout)
|
||||
res.on('end', () => console.log('\n'))
|
||||
})
|
||||
|
||||
req.on('error', (err) => console.error('Error: ', err))
|
||||
|
||||
const parts = files.map((file, i) => (cb) => {
|
||||
const stream = fs.createReadStream(file)
|
||||
stream.once('open', () => {
|
||||
req.write(
|
||||
`\r\n--${boundary}\r\n` +
|
||||
'Content-Disposition: ' +
|
||||
`form-data; name="userfile${i}";` +
|
||||
`filename="${path.basename(file)}"\r\n` +
|
||||
'Content-Type: application/octet-stream\r\n' +
|
||||
'Content-Transfer-Encoding: binary\r\n' +
|
||||
'\r\n'
|
||||
)
|
||||
})
|
||||
stream.pipe(req, {end: false})
|
||||
stream.on('data', (chunk) => req.write(chunk))
|
||||
stream.on('end', cb)
|
||||
})
|
||||
|
||||
steed.series(parts, () => req.end(`\r\n--${boundary}--\r\n`))
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "multipart-post-uploads",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"steed": "^1.1.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
'use strict'
|
||||
|
||||
const http = require('http')
|
||||
const payload = `{
|
||||
"name": "Cian Ó Maidín",
|
||||
"company": "nearForm"
|
||||
}`
|
||||
const opts = {
|
||||
method: 'POST',
|
||||
hostname: 'reqres.in',
|
||||
port: 80,
|
||||
path: '/api/users',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(payload)
|
||||
}
|
||||
}
|
||||
|
||||
const req = http.request(opts, (res) => {
|
||||
console.log('\n Status: ' + res.statusCode)
|
||||
process.stdout.write(' Body: ')
|
||||
res.pipe(process.stdout)
|
||||
res.on('end', () => console.log('\n'))
|
||||
})
|
||||
|
||||
req.on('error', (err) => console.error('Error: ', err))
|
||||
|
||||
req.end(payload)
|
||||
@@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
const http = require('http')
|
||||
const opts = {
|
||||
method: 'POST',
|
||||
hostname: 'reqres.in',
|
||||
port: 80,
|
||||
path: '/api/users',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Transfer-Encoding': 'chunked'
|
||||
}
|
||||
}
|
||||
|
||||
const req = http.request(opts, (res) => {
|
||||
console.log('\n Status: ' + res.statusCode)
|
||||
process.stdout.write(' Body: ')
|
||||
res.pipe(process.stdout)
|
||||
res.on('end', () => console.log('\n'))
|
||||
})
|
||||
|
||||
req.on('error', (err) => console.error('Error: ', err))
|
||||
|
||||
http.get('http://reqres.in/api/users', (res) => {
|
||||
res.pipe(req)
|
||||
})
|
||||
Reference in New Issue
Block a user