How to upload images via API with Node.js?

This has been asked various times, but none of the solutions so far work for me.

I’m using Node.js to upload images for a post via the API.

I can create posts no problem, but images will not upload.

I convert the image (a png file) into a binary string.

Then I post an application/json request with the API creds in the headers, and a payload like this:

   type: "composer",
   synchronous: true,
   file: `${binary_string}`

I’ve tried both file as the field name for the file as the API docs say, and also files[] as another post in the Forum suggested.

Using file, I get a 422 with:

undefined method `tempfile' for #<String:0x00007fdfeba2b1d8>

Using files[], I get a 422 error, with “Unprocessable Entity”.

As well as application/json as the docs say, I’ve also tried it as multipart/form-data as the Web UI does and this poster did.

For the form-data post, I did it like this:

 const form = new FormData();
 form.append("type", "composer");
 form.append("synchronous", "true");
 form.append("files[]", this.stringToBinary(!), {
      contentType: file.mimetype,
 });"/uploads.json", form.getBuffer(), {
  headers: form.getHeaders(),
 }) // creds get added

With this one, with either file or files[], I get a 422 with the error:

undefined method `tempfile' for #<String:0x00007fdfeba2b1d8>

Can anyone help?

I got this to work, like this:

import Axios from "axios";
import FormData from "form-data";
import fs from "fs";
const http = Axios.create({
  baseURL: "",
  headers: {
    "Api-Username": "...",
    "Content-Type": "application/json",
    Accept: "application/json",
http.interceptors.request.use((config) => {
  if ( instanceof FormData) {
  return config;

const filename = "/Users/sitapati/Desktop/process.png";
const file = fs.readFileSync(filename);
const form = new FormData();
form.append("files[]", file, {

  .post("/uploads.json", form, {
    params: {
      type: "composer",
      synchronous: true,
  .then(({ data }) => {"Response from Discourse", JSON.stringify(data, null, 2));
    return {
      url: data.url,
  .catch((e) => {
      "Error uploading file to Discourse",
      JSON.stringify(e, null, 2)
    throw e;

It also works with a file retrieved from a remote URL, which needs to be retrieved like this:

Axios.get(file.slackUrl, {
   responseType: "arraybuffer",
   headers: { Authorization: "Bearer " + this.slackToken },

The key here is: responseType: "arraybuffer".