-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86a9275
commit 9b8e74a
Showing
7 changed files
with
144 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var http = require('http') | ||
var https = require('https') | ||
var url = require('url') | ||
|
||
module.exports = function GET(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.get : http.get | ||
|
||
opts.rejectUnauthorized = false | ||
opts.agent = false | ||
opts.headers = options.headers || {} | ||
opts.headers['User-Agent'] = opts.headers['User-Agent'] || 'tiny-http' | ||
opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json' | ||
|
||
// make a request | ||
method(opts, res=> { | ||
|
||
var rawData = '' | ||
var statusCode = res.statusCode | ||
var contentType = res.headers['content-type'] | ||
var isJSON = contentType.startsWith('application/json') | ||
|
||
if (statusCode !== 200) { | ||
callback(Error(`GET failed with: ${statusCode}`)) | ||
res.resume() | ||
return | ||
} | ||
|
||
res.setEncoding('utf8') | ||
res.on('data', chunk=> rawData += chunk) | ||
res.on('end', x=> { | ||
try { | ||
var parsedData = isJSON? JSON.parse(rawData) : rawData | ||
callback(null, parsedData) | ||
} | ||
catch (e) { | ||
callback(e.message) | ||
} | ||
}) | ||
}).on('error', e=> callback(Error(e.message))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var qs = require('querystring') | ||
var http = require('http') | ||
var https = require('https') | ||
var url = require('url') | ||
|
||
module.exports = function POST(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 | ||
|
||
opts.method = 'POST' | ||
opts.rejectUnauthorized = false | ||
opts.agent = false | ||
opts.headers = options.headers || {} | ||
opts.headers['User-Agent'] = opts.headers['User-Agent'] || 'tiny-http' | ||
opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json' | ||
// opts.headers['Content-Type'] = 'application/x-www-form-urlencoded' | ||
var reqJSON = opts.headers['Content-Type'] === 'application/json' | ||
var postData = reqJSON? JSON.stringify(options.data || {}) : qs.stringify(options.data || {}) | ||
|
||
// make a POST request | ||
var req = method(opts, res=> { | ||
|
||
var rawData = '' | ||
var statusCode = res.statusCode | ||
var contentType = res.headers['content-type'] | ||
var isJSON = contentType === 'application/json' | ||
|
||
if (statusCode !== 200) { | ||
callback(Error(`GET failed with: ${statusCode}`)) | ||
res.resume() | ||
return | ||
} | ||
|
||
res.setEncoding('utf8') | ||
res.on('data', chunk=> rawData += chunk) | ||
res.on('end', x=> { | ||
try { | ||
var parsedData = isJSON? JSON.parse(rawData) : rawData | ||
callback(null, parsedData) | ||
} | ||
catch (e) { | ||
callback(e.message) | ||
} | ||
}) | ||
}) | ||
|
||
req.on('error', e=> callback(Error(e.message))) | ||
|
||
req.write(postData) | ||
|
||
req.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,7 @@ | ||
var http = require('http') | ||
var https = require('https') | ||
var url = require('url') | ||
var _get = require('./_get') | ||
var _post = require('./_post') | ||
|
||
function exec(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.get : http.get | ||
|
||
// ignore warnings about dns | ||
opts.rejectUnauthorized = false | ||
opts.agent = false | ||
opts.timeout = 3000 | ||
opts.headers = options.headers || {} | ||
|
||
if (httpMethod === 'POST') { | ||
method = opts.protocol === 'https:'? https.request : http.request | ||
opts.method = 'POST' | ||
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded' | ||
} | ||
|
||
// set a User-Agent (Github requires this for example) | ||
if (!opts.headers['User-Agent']) { | ||
opts.headers['User-Agent'] = 'tiny-http' | ||
} | ||
|
||
// if json and no Content-Type set then use application/json | ||
if (options.json && typeof opts.headers['Content-Type'] === 'undefined') { | ||
opts.headers['Content-Type'] === 'application/json' | ||
} | ||
|
||
console.log(opts) | ||
|
||
// make a request | ||
method(opts, res=> { | ||
|
||
var rawData = '' | ||
var statusCode = res.statusCode | ||
// var contentType = res.headers['content-type'] | ||
|
||
if (statusCode !== 200) { | ||
callback(Error(`GET failed with: ${statusCode}`)) | ||
res.resume() | ||
return | ||
} | ||
|
||
res.setEncoding('utf8') | ||
res.on('data', chunk=> rawData += chunk) | ||
res.on('end', x=> { | ||
try { | ||
var parsedData = options.json? JSON.parse(rawData) : rawData | ||
callback(null, parsedData) | ||
} | ||
catch (e) { | ||
callback(e.message) | ||
} | ||
}) | ||
}).on('error', e=> callback(Error(e.message))) | ||
} | ||
|
||
// - opts: url | ||
module.exports = { | ||
|
||
get(options, callback) { | ||
exec('GET', options, callback) | ||
}, | ||
|
||
post(options, callback) { | ||
exec('POST', options, callback) | ||
} | ||
get: _get, | ||
post: _post, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
minimalist http.get and http.post | ||
# tiny-json-http | ||
|
||
- symmetry with mikeal/request | ||
Minimalist `HTTP` client for `GET` and `POST`ing `JSON` payloads | ||
|
||
- Zero dependencies: perfect for AWS Lambda | ||
- Sane default: assumes buffered JSON responses | ||
- System symmetry: Node style errback API | ||
|
||
```bash | ||
npm i tiny-json-http --save | ||
``` | ||
|
||
### api | ||
|
||
- `tiny.get(options, callback)` | ||
- `tiny.post(options, callback)` | ||
|
||
### options | ||
|
||
- `url` *required* | ||
- `data` only used by `http.post` | ||
- `headers` key/value map used for headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters