In a discussion titled “How to upload files through the API?” under the Dev category with the tag rest-api, JQ331 seeks help with the correct syntax for uploading files via Discourse’s API using AJAX, receiving a 422 error. RGJ asks for more details from the error response, but JQ331 reports no additional logs. blake shares a working Node.js example from a meta.discourse.org post, which JQ331 finds helpful but questions whether reading file contents (as in fs.readFileSync) will work for binary/media files. dennisjbr then provides a working PHP cURL example, including headers, POST fields (using CURLFILE), and sample output showing successful upload metadata such as URL, filename, and file size. The example confirms that file uploads work with multipart/form-data and proper authentication headers (Api-Key and Api-Username).
Thanks very much, @blake. That is a helpful post that I hadn’t seen before (despite searching for one like that). It will help me move forward for sure. One thing caught my eye, though. This line in the linked code: const file = fs.readFileSync(filename);
This line implies that what you upload to discourse is not actually the file itself, but the contents of the file (in the above code, the file variable = the string returned from reading the file). That’s surprising to me, and I’m wondering what that means for uploading binary files (or media files–which upload fine to discourse directly (if size is ok), but not sure how reading them first will work).