├── .gitignore ├── package.json ├── README.md └── lib └── har-to-curl.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | node_modules/ 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "har-to-curl", 3 | "version": "0.5.0", 4 | "description": "A CommonJS utility for converting a HAR (HTTP Archive) format JSON object to a cURL command string for use on the command line.", 5 | "main": "lib/har-to-curl.js", 6 | "implements": ["CommonJS/Modules/1.0"], 7 | "homepage": "https://github.com/mattcg/har-to-curl", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/mattcg/har-to-curl.git" 11 | }, 12 | "contributors": [ 13 | { 14 | "name": "Matthew Caruana Galizia", 15 | "email": "mattcg@gmail.com" 16 | } 17 | ], 18 | "license": "MIT", 19 | "keywords": [ 20 | "curl", 21 | "har", 22 | "convert" 23 | ], 24 | "files": [ 25 | "/lib" 26 | ], 27 | "engines": { 28 | "node": ">=12" 29 | }, 30 | "eslintConfig": { 31 | "parserOptions": { 32 | "ecmaVersion": 13, 33 | "sourceType": "module" 34 | }, 35 | "env": { 36 | "node": true 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HAR to cURL # 2 | 3 | Written in JavaScript. CommonJS format. Inspired by a [Python implementation](https://github.com/snoe/harToCurl). 4 | 5 | :speech_balloon: Try the [web interface](https://mattcg.github.io/har-to-curl/). 6 | 7 | ## Install ## 8 | 9 | Use npm: 10 | 11 | ```bash 12 | npm install har-to-curl 13 | ``` 14 | 15 | ## Example ## 16 | 17 | ```JavaScript 18 | var harToCurl = require('har-to-curl'); 19 | 20 | var myHarString = '{"startedDateTime": "2013-02-21T16:23:17.806Z", "time": 577, "request": { "method": "GET", "url": "http://...'; 21 | var myCurlCommand; 22 | 23 | // Passing in an object: 24 | var myHarObject = JSON.parse(myHarString); 25 | myCurlCommand = harToCurl(myHarObject); 26 | 27 | // Passing in a string - will be JSON.parsed automatically: 28 | myCurlCommand = harToCurl(myHarString); 29 | ``` 30 | 31 | ## License ## 32 | 33 | Copyright © 2012 [Matthew Caruana Galizia](https://twitter.com/mcaruanagalizia), licensed under an [MIT license](http://mattcg.mit-license.org/). 34 | -------------------------------------------------------------------------------- /lib/har-to-curl.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Matthew Caruana Galizia 3 | * @license MIT license 4 | * @copyright Copyright (c) 2012, Matthew Caruana Galizia 5 | */ 6 | 7 | /*jshint node: true */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = harToCurl; 12 | 13 | function harToCurl(har) { 14 | if (typeof har === 'string') { 15 | har = JSON.parse(har); 16 | } 17 | 18 | if (!har || typeof har !== 'object') { 19 | return; 20 | } 21 | 22 | if (har.request) { 23 | return harToCurl.fromEntry(har); 24 | } 25 | 26 | if (har.log && Array.isArray(har.log.entries)) { 27 | return harToCurl.fromLog(har.log); 28 | } 29 | 30 | if (Array.isArray(har)) { 31 | return harToCurl.fromEntries(har); 32 | } 33 | 34 | if (Array.isArray(har.entries)) { 35 | return harToCurl.fromLog(har); 36 | } 37 | } 38 | 39 | harToCurl.fromLog = function(log) { 40 | if (!log || !Array.isArray(log.entries)) { 41 | return; 42 | } 43 | 44 | return harToCurl.fromEntries(log.entries); 45 | }; 46 | 47 | harToCurl.fromEntries = function(entries) { 48 | return entries.map(harToCurl.fromEntry); 49 | }; 50 | 51 | harToCurl.fromEntry = function(entry) { 52 | var command, request; 53 | 54 | if (!entry || !entry.request) { 55 | return ''; 56 | } 57 | 58 | request = entry.request; 59 | command = 'curl -X ' + request.method; 60 | 61 | if (request.httpVersion === 'HTTP/1.0') { 62 | command += ' -0'; 63 | } 64 | 65 | if (request.cookies.length) { 66 | command += ' -b \'' + request.cookies.map(function(cookie) { 67 | return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value); 68 | }).join('&') + '\''; 69 | } 70 | 71 | command += request.headers.map(function(header) { 72 | return ' -H \'' + header.name + ': ' + header.value + '\''; 73 | }).join(''); 74 | 75 | if (request.postData) { 76 | if (request.postData.text) { 77 | command += ' -d \'' + request.postData.text + '\''; 78 | } else if (request.postData.params && request.postData.params.length > 0) { 79 | command += ' -d \'' + request.postData.params.map(function(param) { 80 | return param.name + '=' + param.value; 81 | }).join('&') + '\''; 82 | } 83 | } 84 | 85 | return command + ' \'' + encodeURI(request.url) + '\''; 86 | }; 87 | --------------------------------------------------------------------------------