├── .gitignore ├── etc └── curl.png ├── bin ├── ffcurl └── fcurl ├── History.md ├── package.json ├── Readme.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /etc/curl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstacruz/curlformat/master/etc/curl.png -------------------------------------------------------------------------------- /bin/ffcurl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | process.argv.push('--extended'); 3 | require('./fcurl') 4 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | ## v0.1.0 2 | 3 | * Support JSON data. 4 | * Ignore Content-Length header. 5 | 6 | ## v0.0.2 - August 13, 2014 7 | 8 | * Quote URLs. 9 | * Add `ffcurl` as an alias for `fcurl --extended`. 10 | 11 | ## v0.0.1 - August 9, 2014 12 | 13 | * Yo. 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "curlformat", 3 | "version": "0.1.0", 4 | "description": "CLI utility to clean up your 'Copy as cURL' strings", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "bin": { 10 | "fcurl": "./bin/fcurl", 11 | "ffcurl": "./bin/ffcurl" 12 | }, 13 | "author": "Rico Sta. Cruz ", 14 | "license": "MIT", 15 | "dependencies": { 16 | "minimist": "^0.2.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /bin/fcurl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var args = require('minimist')(process.argv.slice(2), { 4 | string: ['request', 'header', 'data'], 5 | boolean: ['simple', 'get'], 6 | alias: { 7 | h: 'help', v: 'version', 8 | X: 'request', H: 'header', S: 'simple', d: 'data', 9 | G: 'get' 10 | } 11 | }); 12 | 13 | if (args.help || !args._[0]) { 14 | var cmd = require('path').basename(process.argv[1]); 15 | console.log([ 16 | 'Usage:', 17 | ' '+cmd+' [options]', 18 | '', 19 | 'Options:', 20 | ' -h, --help print usage information', 21 | ' -v, --version show version info and exit', 22 | ' --extended show more stuff', 23 | '', 24 | 'Curl options:', 25 | ' -H, --header ...', 26 | ' -d, --data ...', 27 | ' -X, --request ...', 28 | ' -G, --get ...', 29 | ].join('\n')); 30 | process.exit(); 31 | } 32 | 33 | if (args.version) { 34 | console.log(require('../package.json').version); 35 | process.exit(); 36 | } 37 | 38 | var Request = require('../index'); 39 | console.log(new Request(args).toString()); 40 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # npm install -g curlformat 2 | 3 | I like to do this in Firefox or Chrome. The output is unreadable though. 4 | 5 | ![image](etc/curl.png) 6 | 7 | So what? 8 | -------- 9 | 10 | This untangles that. Type `f` in your terminal, then paste the curl command. 11 | Your command line should look like this: 12 | 13 | ```sh 14 | $ fcurl 'http://site.com/article/new' -H 'Host: site.com' -H 'Connection: 15 | keep-alive' -H 'Accept-Language: en-us' -d "title=Hello&body=Welcome%20to%20" ... 16 | ``` 17 | 18 | BAM! Now its readable! 19 | 20 | ```sh 21 | http POST "http://site.com/article/new" \ 22 | title="Hello" \ 23 | body="Welcome to my site!" 24 | ``` 25 | 26 | That's sweet. 27 | ------------- 28 | 29 | I know. Now pass `--extended` to make print more stuff that would've been supressed. 30 | 31 | ```sh 32 | http OPTIONS "http://site.com/users" \ 33 | Connection:"keep-alive" \ 34 | Access-Control-Request-Method:"GET" \ 35 | Origin:"http://site.com" \ 36 | Accept-Encoding:"gzip, deflate" \ 37 | ... 38 | ``` 39 | 40 | Cool beans 41 | ---------- 42 | 43 | Oh and you can also install [httpie]. The output of `curlformat` is compatible with httpie. 44 | 45 | [httpie]: http://httpie.org 46 | 47 | ## :copyright: 48 | 49 | **curlformat** © 2014+, Rico Sta. Cruz. Released under the [MIT License].
50 | Authored and maintained by Rico Sta. Cruz with help from [contributors]. 51 | 52 | > [ricostacruz.com](http://ricostacruz.com)  ·  53 | > GitHub [@rstacruz](https://github.com/rstacruz)  ·  54 | > Twitter [@rstacruz](https://twitter.com/rstacruz) 55 | 56 | [MIT License]: http://mit-license.org/ 57 | [contributors]: http://github.com/rstacruz/curlformat/contributors 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var qsparse = require('querystring').parse; 2 | var extend = require('util')._extend; 3 | 4 | function Request (args) { 5 | var self = this; 6 | 7 | // workaround 8 | if (args._ && args._[0] === 'curl') 9 | args._ = args._.slice(1); 10 | 11 | this.data = {}; 12 | this.method = 'GET'; 13 | this.url = args._[0]; 14 | this.headers = {}; 15 | 16 | if (args.header) { 17 | extend(this.headers, toArr(args.header) 18 | .reduce(function (obj, header) { 19 | var m = header.split(': '); 20 | obj[m[0]] = m[1]; 21 | return obj; 22 | }, {})); 23 | } 24 | 25 | if (!args.extended) { 26 | Request.ignoreHeaders.forEach(function (key) { 27 | delete(self.headers[key]); 28 | }); 29 | } 30 | 31 | if (args.data) { 32 | if (args.data.match(/^[\{\[].*[\]\}]$/)) 33 | extend(this.data, JSON.parse(args.data)); 34 | else 35 | extend(this.data, qsparse(args.data)); 36 | this.method = 'POST'; 37 | } 38 | 39 | if (args.get) { 40 | this.method = 'GET'; 41 | } 42 | 43 | if (~this.url.indexOf('?')) { 44 | var qs = this.url.split('?'); 45 | this.url = qs[0]; 46 | extend(this.data, qsparse(qs[1])); 47 | } 48 | 49 | if (args.request) 50 | this.method = args.request.toUpperCase(); 51 | 52 | if (!this.url) 53 | throw new Error("No URL"); 54 | } 55 | 56 | Request.ignoreHeaders = [ 57 | 'Host', 'User-Agent', 'Accept', 'Accept-Language', 'Accept-Encoding', 58 | 'Origin', 'Connection', 'X-Requested-With', 'Content-Type', 'Pragma', 59 | 'Cache-Control', 'Referer', 'Cookie', 'Content-Length' 60 | ]; 61 | 62 | Request.prototype.toString = function () { 63 | var cmd = 'http ' + this.method + ' ' + quote(this.url); 64 | var cr = ' \\\n'; 65 | var args = []; 66 | var self = this; 67 | 68 | if (self.data) { 69 | Object.keys(self.data).forEach(function (key) { 70 | args.push(' ' + key + '=' + quote(self.data[key])); 71 | }); 72 | } 73 | 74 | if (self.headers) { 75 | Object.keys(self.headers).forEach(function (key) { 76 | args.push(' ' + key + ':' + quote(self.headers[key])); 77 | }); 78 | } 79 | 80 | if (args.length) 81 | return cmd + cr + args.join(cr); 82 | else 83 | return cmd; 84 | }; 85 | 86 | module.exports = Request; 87 | 88 | function toArr (val) { 89 | if (Array.isArray(val)) return val; 90 | if (!val) return []; 91 | else return [val]; 92 | } 93 | 94 | function quote (str) { 95 | return JSON.stringify(str); 96 | } 97 | --------------------------------------------------------------------------------