I managed to succeed in uploading files through my own custom script by adding the “X-Request-With” header. This makes the final curl command something like:
curl -k -T testfile.txt -u "AtAxVrKorgC5YJf:" -H 'X-Requested-With: XMLHttpRequest' https://nextcloud.example.com/public.php/webdav/testfile.txt
-u is the parameter for the “Authorization: Basic” header where the username is the final part of the share link created by nextcloud. This makes sure the file gets uploaded to the correct directory.