Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CLI utility for har-to-curl. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ myCurlCommand = harToCurl(myHarObject);
myCurlCommand = harToCurl(myHarString);
```

## CLI Usage ##
```Bash
$ (set -x; har-to-curl /tmp/HarFiles/013b0a68-825b-4303-ba3a-ae6167d1b89c.har | jq -r '.[0]' | bash -x | jq)
+ har-to-curl /tmp/HarFiles/013b0a68-825b-4303-ba3a-ae6167d1b89c.har
+ jq -r '.[0]'
+ bash -x
+ jq
+ curl -X POST -H 'X-Correlation-Id: 013b0a68-825b-4303-ba3a-ae6167d1b89c' http://localhost:3000/api/coffee
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 20 100 20 0 0 3 0 0:00:06 0:00:05 0:00:01 5
{
"status": "success"
}
```

## License ##

Copyright © 2012 [Matthew Caruana Galizia](https://twitter.com/mcaruanagalizia), licensed under an [MIT license](http://mattcg.mit-license.org/).
36 changes: 36 additions & 0 deletions bin/har-to-curl-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node
const harToCurl = require('../lib/har-to-curl');
const fs = require('fs');
const path = require('path');

// Command-line arguments
const [,, harPath] = process.argv;


// Check if HAR file path is provided
if (!harPath) {
console.error('Error: Please provide the path to the HAR file.');
process.exit(1);
}


// Validate and read the HAR file
const absoluteHarPath = path.resolve(harPath);
if (!fs.existsSync(absoluteHarPath)) {
console.error(`Error: File ${absoluteHarPath} does not exist.`);
process.exit(1);
}

const harContent = fs.readFileSync(absoluteHarPath, 'utf8');
const harJson = JSON.parse(harContent);

// Convert HAR to cURL using your library
const curlCommand = harToCurl(harJson);

// Output the cURL command
if (curlCommand) {
console.log(curlCommand);
} else {
console.error('Error: Failed to convert HAR to cURL.');
process.exit(1);
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"files": [
"/lib"
],
"bin": {
"har-to-curl": "./bin/har-to-curl-cli.js"
},
"engines": {
"node": ">=12"
},
Expand Down