forked from brianleroux/tiny-json-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_write.js
69 lines (56 loc) · 1.97 KB
/
_write.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var qs = require('querystring')
var http = require('http')
var https = require('https')
var url = require('url')
module.exports = function _write(httpMethod, options, callback) {
// require options.url or fail noisily
if (!options.url) {
throw Error('options.url required')
}
// parse out the options from options.url
var opts = url.parse(options.url)
var method = opts.protocol === 'https:'? https.request : http.request
var defaultContentType = 'application/json; charset=utf-8'
// put the params on the query as well as the body?
if (httpMethod === 'DELETE' && options.data) {
var isSearch = !!opts.search
options.url += (isSearch? '&' : '?') + qs.stringify(options.data)
opts = url.parse(options.url)
}
opts.method = httpMethod
opts.rejectUnauthorized = false
opts.headers = options.headers || {}
opts.headers['User-Agent'] = opts.headers['User-Agent'] || 'tiny-http'
opts.headers['Content-Type'] = opts.headers['Content-Type'] || defaultContentType
var reqJSON = opts.headers['Content-Type'].startsWith('application/json')
var postData = reqJSON? JSON.stringify(options.data || {}) : qs.stringify(options.data || {})
// make a POST request
var req = method(opts, function(res) {
var raw = [] // keep our buffers here
var ok = res.statusCode >= 200 && res.statusCode < 300
if (!ok) {
callback(Error(httpMethod + ' failed with: ' + res.statusCode))
res.resume()
return
}
res.on('data', function __data(chunk) {
raw.push(chunk)
})
res.on('end', function __end() {
var err = null
var result = null
try {
var isJSON = res.headers['content-type'].startsWith('application/json')
var rawData = Buffer.concat(raw).toString()
result = isJSON? JSON.parse(rawData) : rawData
}
catch (e) {
err = e
}
callback(err, {body:result, headers:res.headers})
})
})
req.on('error', callback)
req.write(postData)
req.end()
}