Comparing version
{ | ||
"name": "http-tool", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A command-line tool for interacting with web sites and APIs", | ||
"main": "src/index.js", | ||
"bin": { | ||
"http-tool": "src/index.js" | ||
}, | ||
"scripts": { | ||
@@ -28,7 +31,12 @@ "test": "mocha test" | ||
"homepage": "https://github.com/joeattardi/http-tool#readme", | ||
"engines": { "node": ">=4.0.0" }, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
"chalk": "^1.1.3", | ||
"debug": "^2.2.0", | ||
"jsome": "^2.3.25", | ||
"lodash": "^4.16.4", | ||
"request": "^2.75.0" | ||
"request": "^2.75.0", | ||
"yargs": "^6.3.0" | ||
}, | ||
@@ -35,0 +43,0 @@ "devDependencies": { |
# http-tool | ||
A command-line tool for interacting with web sites and APIs | ||
http-tool is a command-line utility like curl or HTTPie, used for making HTTP requests. | ||
# Installation | ||
Make sure you have Node 4 or newer, and run `npm install -g http-tool`. | ||
# Usage | ||
Usage: `http-tool [options] <URL>` | ||
## Options | ||
### HTTP method | ||
To specify which HTTP method to use (`GET`, `POST`, `DELETE`, etc.), use the `--method` or `-m` option. The default is `GET`. | ||
### Custom request headers | ||
To include custom headers in the HTTP request, use the `--header` or `-H` option. The header should be specified as a `key: value` pair as a string, e.g. `-H "Content-Type: application/json`. You can use the `--header` or `-H` option multiple times for multiple custom headers. | ||
### Output options | ||
* `--headers-only, -r`: Only include the response headers in the output | ||
* `--body-only, -b`: Only include the response body in the output |
@@ -0,19 +1,81 @@ | ||
#! /usr/bin/env node | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const request = require('request'); | ||
const chalk = require('chalk'); | ||
const jsome = require('jsome'); | ||
const debug = require('debug')('index'); | ||
const pkg = require('../package.json'); | ||
const outputFormatter = require('./output-formatter'); | ||
const args = require('./cli-args'); | ||
const headers = require('./headers'); | ||
const url = process.argv[2]; | ||
function validateUrl() { | ||
let url = args._[0]; | ||
if (!url) { | ||
console.error('You didn\'t specify a URL'); | ||
process.exit(1); | ||
} else if (!url.match(/^https?:\/\//)) { | ||
url = `http://${url}`; | ||
} | ||
return url; | ||
} | ||
function handleError(error) { | ||
if (error.syscall === 'getaddrinfo' && error.errno === 'ENOTFOUND') { | ||
console.error(`Unable to resolve host ${error.hostname}`); | ||
} else if (error.syscall === 'connect' && error.errno === 'ECONNREFUSED') { | ||
console.error(`Unable to connect to ${options.url}: Connection refused`); | ||
} else { | ||
console.error('An unexpected error has occurred.'); | ||
console.error(error); | ||
} | ||
process.exit(1); | ||
} | ||
const startTime = Date.now(); | ||
request(url, (error, response, body) => { | ||
console.log(outputFormatter.formatStatusLine(response)); | ||
console.log(''); | ||
console.log(outputFormatter.formatHeaders(response.rawHeaders)); | ||
console.log(''); | ||
console.log(body); | ||
const options = { | ||
method: args.method, | ||
url: validateUrl(), | ||
headers: { | ||
'User-Agent': `http-tool/${pkg.version}` | ||
} | ||
}; | ||
if (args.data) { | ||
options.body = args.data; | ||
} | ||
headers.processHeaders(args.header, options); | ||
debug('Using options:', options); | ||
request(options, (error, response, body) => { | ||
if (error) { | ||
handleError(error); | ||
} | ||
if (!args['body-only']) { | ||
console.log(outputFormatter.formatStatusLine(response)); | ||
console.log(''); | ||
console.log(outputFormatter.formatHeaders(response.rawHeaders)); | ||
console.log(''); | ||
} | ||
if (!args['headers-only']) { | ||
const contentType = response.headers['content-type']; | ||
if (contentType.indexOf('application/json') === 0) { | ||
jsome.parse(body); | ||
} else { | ||
console.log(body); | ||
} | ||
} | ||
const endTime = Date.now(); | ||
@@ -20,0 +82,0 @@ console.log(`Completed in ${(endTime - startTime) / 1000} sec.`); |
Sorry, the diff of this file is not supported yet
84924
1882.82%12
71.43%193
171.83%22
633.33%6
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added