API - receive json data and write to file

:wave:Trying to figure out how to receive data from external source, write it into a file and process with an external script. First time approaching something like that, I would deeply appreciate some guidance. This is how I see it at present:

You can actually combine step 1 and 2, there is no need to load a file first and then call some script to process it. You can just send the data to a script that processes it.

What you do is you create a route (appinfo/routes.php) which calls a php function in a controller (lib/Controller/Whatever.php) – Tutorial — Nextcloud latest Developer Manual latest documentation

The data you provide via GET/POST/PUT will be provided to the function in the controller as parameters, which you can then process. You can add flags to your function based on the kind of security / access control you want, which by default will require an authenticated administrator, but, for example, you can flag @NoAdminRequired to change that to ANY authenticated user, or @PublicPage to open the script to the public and/or perform your own security checks, etc.

For example, postdata=“data=something&username=jimmy” → public function myfunction(string $data, string $username){…}
The variables in the postdata will be available via corresponding variables in the controller’s function.

You can also upload a file to your controller and access it directly rather than having it broken up as parameters;
https://docs.nextcloud.com/server/latest/developer_manual/basics/controllers.html#reading-headers-files-cookies-and-environment-variables

1 Like