# How to upload image using Discourse API

**URL:** https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869
**Category:** Development
**Tags:** rest-api
**Created:** [April 22, 2020, 3:23pm UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869 "2020-04-22T15:23:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Saurabh\_Khandelwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saurabh_khandelwal/32/156332_2.png) [@Saurabh\_Khandelwal](https://meta.discourse.org/u/Saurabh_Khandelwal)
#### Post date: [April 22, 2020, 3:23pm UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869/1 "2020-04-22T15:23:55Z")

</div>

Hello All,

I’m trying to upload image using below discourse api through JAVA code:  
[https://discourse.example.com/uploads.json](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.

---

<div class="post-metadata">

### Author: ![dhanya](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@dhanya](https://meta.discourse.org/u/dhanya)
#### Post date: [September 22, 2020, 4:09pm UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869/2 "2020-09-22T16:09:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [September 22, 2020, 4:23pm UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869/3 "2020-09-22T16:23:19Z")

</div>

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

> [@Upload avatar image with API](https://meta.discourse.org/t/upload-avatar-image-with-api/55253/7):
>
> Okay I just verified that I got this to work. You just need to add the synchronous: true value to get the id. now that we have the id: 17 we can then make one more request:

---

<div class="post-metadata">

### Author: ![dhanya](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@dhanya](https://meta.discourse.org/u/dhanya)
#### Post date: [September 22, 2020, 4:36pm UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869/4 "2020-09-22T16:36:32Z")

</div>

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.json`resulted in a`422 Unprocessable Entity`response: {"failed":"FAILED","message":"undefined method`tempfile’ for #\u003cArray:0x00007f0d5c784810\u003e"}

Trying integration with Drupal to Discourse.

---

<div class="post-metadata">

### Author: ![jwulf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jwulf/32/202597_2.png) [@jwulf](https://meta.discourse.org/u/jwulf)
#### Post date: [December 8, 2020, 7:09am UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869/5 "2020-12-08T07:09:34Z")

</div>

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](https://docs.discourse.org/#tag/Upload), it says the content-type for the request is `application/json`.

In [this post](https://meta.discourse.org/t/upload-avatar-image-with-api/55253/7), 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?

---

<div class="post-metadata">

### Author: ![CormacW43](https://avatars.discourse-cdn.com/v4/letter/c/919ad9/32.png) [@CormacW43](https://meta.discourse.org/u/CormacW43)
#### Post date: [August 28, 2024, 11:47am UTC](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869/6 "2024-08-28T11:47:26Z")

</div>

This still doesn’t appear to be working.

This is the error I get when trying to upload an image, in binary that is.

```plaintext
Unprocessable Entity - {\"failed\":\"FAILED\",\"message\":\"undefined method `tempfile' for an instance of String\"}

```
