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

Default Accept header to 'application/json' #3

Open
apm963 opened this issue Jul 2, 2019 · 1 comment
Open

Default Accept header to 'application/json' #3

apm963 opened this issue Jul 2, 2019 · 1 comment

Comments

@apm963
Copy link
Contributor

apm963 commented Jul 2, 2019

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
Copy link
Contributor Author

apm963 commented Jul 12, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant