How to upload image using Discourse API

Hello All,

I’m trying to upload image using below discourse api through JAVA code:
https://discourse.example.com/uploads.json
by passing json in Body:
{
“type” : “avatar”,
“user_id”: 1,
“synchronous”: true,
“file”: “Binary String”
}

But in response I’m getting below result:

{
“failed”: “FAILED”,
“message”: “undefined method `tempfile’ for #String:0x000055fc2e0a01d8
}

Where I’m doing wrong?
Please help me.

Experiancing the same problem. @Saurabh_Khandelwal if you are able to find a solution, Please reply me.

1 Like

The parameter for file needs to be called files[]. See this response for an example using Postman:

This is my sample data
Array
(
[type] => avatar
[user_id] => 3
[synchronous] => 1
[file] => 1101000 1110100 1110100 1110000 1110011 111010 101111 101111 1101011 1101001 1111000 101101 1100100 1100101 1110110 101110 1100001 1110011 1110011 1111001 1110011 1110100 101101 1110101 1100011 101110 1100011 1101111 1101101 101111 1110011 1101001 1110100 1100101 1110011 101111 1100100 1100101 1100110 1100001 1110101 1101100 1110100 101111 1100110 1101001 1101100 1100101 1110011 101111 1110000 1101001 1100011 1110100 1110101 1110010 1100101 1110011 101111 110010 110000 110010 110000 101101 110000 110111 101111 1101011 1101001 1111000 1110100 1100101 1100001 1101101 1011111 1101001 1101101 1100111 110001 101110 1101010 1110000 1100111
)

Sending file as binary string. Getting response as given below. This is drupal

/uploads.jsonresulted in a422 Unprocessable Entityresponse: {"failed":"FAILED","message":"undefined methodtempfile’ for #\u003cArray:0x00007f0d5c784810\u003e"}

Trying integration with Drupal to Discourse.

Did you get this to work? I have been trying all manner of combinations with Node.js, and cannot get this to work.

I get a 422 Unprocessable Entity error.

In the Discourse docs, it says the content-type for the request is application/json.

In this post, the Postman example is using form-data, and when I watch the composer in the Discourse Web UI, it uses a multi-part post.

Anyone know how to do this via the API without using the Ruby client?