Open dansteren opened 4 years ago
Are PRs welcome for this one? If yes, high level description of how this should be done would be nice.
Thanks @simonmulser - a PR for this would be great. The payload flag is parsed here: https://github.com/netlify/cli/blob/66fd64c0da3756ec6f91ed6c8f7f5f00883a882a/src/commands/functions/invoke.js#L122
The reason we enforce a JSON payload at the moment is because it is required for event triggered functions (e.g. identity-signup
):
https://github.com/netlify/cli/blob/66fd64c0da3756ec6f91ed6c8f7f5f00883a882a/src/commands/functions/invoke.js#L63
We could enforce a JSON payload only when a function matches an event triggered function and not for all functions. Does that makes sense?
- Do you want to request a feature or report a bug?
Feature Request. It might be considered a bug, but the documentation only makes mention of JSON payloads, so I'm going to assume non-JSON payloads aren't officially supported.
- What is the current behavior?
Calling
netlify functions:invoke
and passing something other than JSON to the--payload
argument results in the payload not being added to the request body.For example, calling
netlify functions:invoke myFunction --payload 'myPayload.txt'
where myPayload.txt contains the stringthisLineIsExecutedNotRead
results in the following error:This makes it impossible to use
netlify functions:invoke
if you need to pass a generic string.- If the current behavior is a bug, please provide the steps to reproduce.
netlify functions:invoke
netlify functions:invoke myFunction --payload 'hello world'
netlify functions:invoke myFunction --payload './myPayload.txt
(where myPayload.txt contains a string of random characters, not a JSON string)- What is the expected behavior?
netlify functions:invoke myFunction --payload 'hello world'
should create a request with the stringhello world
in the body.netlify functions:invoke myFunction --payload './myPayload.txt'
should create a request with the contents of the file as the body.Looking at the code I'm seeing that the method
processPayloadFromFlag
in invoke.js expects the payload to be a JSON string. I think that assumption should not be made. I'm also seeing this on line 156I think rather than trying to require the file (which works for JSON and JS) it should just read the file. This wouldn't evaluate JS anymore, but would be better for JSON, XML, or anything other than JS really. This way the user could specify their body in whatever format they like and know that the contents of their file is the contents of the body of their request.
If the file was read rather than required then it also necessitate a change like this where the function is invoked:
Note: If the expected behavior is that
.js
files should be required then the file extension could be used to differentiate between js and non-js files. That way .js files could be required, and .json, .txt, .xml, etc files could be read instead.- Local Environment Information
────────────────────┐ Environment Info │ ────────────────────┘
System: OS: macOS 10.15.4 CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz Binaries: Node: 14.4.0 - /usr/local/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.4 - /usr/local/bin/npm Browsers: Chrome: 83.0.4103.116 Firefox: 72.0.1 Safari: 13.1 npmGlobalPackages: netlify-cli: 2.54.0
netlify-cli/2.54.0 darwin-x64 node-v14.4.0