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

Passing JSON as an argument #2269

Closed
alysonvilela opened this issue Oct 13, 2024 · 2 comments
Closed

Passing JSON as an argument #2269

alysonvilela opened this issue Oct 13, 2024 · 2 comments

Comments

@alysonvilela
Copy link

alysonvilela commented Oct 13, 2024

I'm trying to pass a full JSON as an argument just like curl does with the --data-raw attribute, but seems like node(or powershell) just remove double quotes.

Example how it does work on CURL CLI

curl 'http://localhost:3000/register' \
  --data-raw '{"id":null,"user":null,"reason":"","realtyTYpe":"","minValue":0,"maxValue":0,"parkings":null,"attributes":[]}'

How its running

❯ pnpm dev validate --schema register_todo --payload '{"teste":"teste"}'                                                                  
> [email protected] dev C:\workspace\alyson\study\cli-test
> tsx watch src/main.ts "validate" "--schema" "register_todo" "--payload" "{teste:teste}"
// Attention on the last line where double quotes just gone.

Result

{ schema: 'register_todo', payload: '{teste:teste}' }

My way to workaround through this is pass a decodeURI on JSON strigified, then in the application I decode, parse the JSON then use it. But we know that this is just too much.

PS: Already got people making a recommendation to add slashes before double quotes like '{\"teste\":\"teste\"}'(and it works) but this is not how CURL works, my goal here is just pass a real stringified JSON

@shadowspawn
Copy link
Collaborator

Each layer of parsing may need assistance to preserve the quotes that matter. Also different shells (and parsers) treat double quotes and single quotes differently.

You are expecting it to work like it does for Curl, and maybe it will if you call the tsx command directly. That would be worth trying.

One extra layer you have in full workflow is also going through pnpm (and whatever is in the script). That is an extra layer of parsing which may have an impact. There isn't a magic solution that will work for any combination of layers of parsing. Try working through each layer and see what the arguments look like after each parsing stage.

It is unlikely to be node itself that is the problem. Node just makes available the arguments it got passed without modification. Likewise, Commander just uses the arguments that node got given and does not do anything with quotes.

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

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

2 participants