curlx
curlx copied to clipboard
Default Accept header to 'application/json'
Currently the Accept header is not set by default, which is fine by itself. However, most of curlx expects the response to be in JSON format. I think it would be beneficial to set the Accept header to application/json by default and allow users to change or unset it.
Alternately, this could be a toggleable / configurable setting.
Here is an example of how this would be implemented:
index.js
module.exports = () => {
let unsanitizedCmdArgs = process.argv.slice(2);
const unsanitizedArgs = minimist(unsanitizedCmdArgs);
if (
!('H' in unsanitizedArgs)
|| (typeof unsanitizedArgs.H === 'object' && 'length' in unsanitizedArgs.H ? unsanitizedArgs.H : [unsanitizedArgs.H])
.filter(s => /Accept:\s*application\/json/i.test(s)).length === 0
) {
unsanitizedCmdArgs = [
'-H',
'Accept: application/json',
...unsanitizedCmdArgs
];
}
const cmdArgs = sanitizeCurlArgs(unsanitizedCmdArgs);
const args = minimist(cmdArgs);
unsanitizedArgs.H can be either undefined (if no -H arg used), a string (if only one -H arg is used) or an array (if more than one -H arg is used). This is the reason for the complexity of the condition.