Skip to content

Instantly share code, notes, and snippets.

@ithinkihaveacat
Last active October 22, 2018 14:01
Show Gist options
  • Save ithinkihaveacat/10800e8f0271513e592cd69326ee6ec3 to your computer and use it in GitHub Desktop.
Save ithinkihaveacat/10800e8f0271513e592cd69326ee6ec3 to your computer and use it in GitHub Desktop.
Extracts HTTP headers from curl command-line args
// convert headers object to object
Array.from(headers).reduce((a: { [k: string]: string }, kv) => (a[kv[0]] = kv[1], a), {})
// parse curl-compatible -H arguments in command-line into object
function seq(first: number, last: number): number[] {
if (first < last) {
return [first].concat(seq(first + 1, last));
} else if (first > last) {
return [last].concat(seq(first, last - 1));
} else {
return [first];
}
}
const headers = seq(2, process.argv.length - 1)
.filter(n => process.argv[n] === "-H")
.map(n => process.argv[n + 1])
.map(s => {
const [h, ...v] = s.split(": ");
return [h, v.join("")];
})
.reduce((a: {[key: string]: any}, kv) => {
a[kv[0]] = kv[1];
return a;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment