shivkanthb / curlx

◼️ Supercharge curl with history, collections and more.
https://curlx.dev
263 stars 5 forks source link

Default Accept header to 'application/json' #3

Open apm963 opened 5 years ago

apm963 commented 5 years ago

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.

apm963 commented 5 years ago

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.