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(file.data!), {
contentType: file.mimetype,
});
http.post("/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?