# Add images to API posts

**URL:** https://meta.discourse.org/t/add-images-to-api-posts/204651
**Category:** Development
**Tags:** rest-api
**Created:** [September 28, 2021, 2:07pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651 "2021-09-28T14:07:43Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![FunnySmile](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/funnysmile/32/229095_2.png) [@FunnySmile](https://meta.discourse.org/u/FunnySmile)
#### Post date: [September 28, 2021, 2:07pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/1 "2021-09-28T14:07:44Z")

</div>

How to add pictures or file to an upcoming topic?

---

<div class="post-metadata">

### Author: ![mapengfei-nwpu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mapengfei-nwpu/32/203450_2.png) [@mapengfei-nwpu](https://meta.discourse.org/u/mapengfei-nwpu)
#### Post date: [January 14, 2022, 5:50pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/2 "2022-01-14T17:50:31Z")

</div>

have you solved this problem?

---

<div class="post-metadata">

### Author: ![mapengfei-nwpu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mapengfei-nwpu/32/203450_2.png) [@mapengfei-nwpu](https://meta.discourse.org/u/mapengfei-nwpu)
#### Post date: [January 14, 2022, 6:44pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/3 "2022-01-14T18:44:59Z")

</div>

You should upload the file and get the url to the file, then post it.

```plaintext
r = requests.put()
p = "http:"+r.json()['url']

```

> [@Discourse API Uploads](https://meta.discourse.org/t/discourse-api-uploads/134545/2):
>
> This worked for me: r = requests.post(f'{rooturl}/uploads.json', files = {'files[]': (file, open(file, 'rb'), 'image/png')}, data={'type':'image'}, headers={ "Api-Username" : sys.argv[1], "Api-Key" : sys.argv[2], }) Probably, content-type': 'multipart/form-data' should be omitted so the requests library can figure out what to.

---

<div class="post-metadata">

### Author: ![FunnySmile](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/funnysmile/32/229095_2.png) [@FunnySmile](https://meta.discourse.org/u/FunnySmile)
#### Post date: [January 17, 2022, 12:47am UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/4 "2022-01-17T00:47:31Z")

</div>

Hey, I tried using a third-party cloud bed to store images later, and then I used img tags to implement content in themes or posts because it saved space on the server.

---

<div class="post-metadata">

### Author: ![Chittaranjan\_Jena](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chittaranjan_jena/32/260683_2.png) [@Chittaranjan\_Jena](https://meta.discourse.org/u/Chittaranjan_Jena)
#### Post date: [July 24, 2022, 3:39pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/5 "2022-07-24T15:39:20Z")

</div>

Hi @FunnySmile I am trying to post an image to post using the img tag like below but it’s not showing?

```plaintext
<img src="https://unsplash.com/photos/JoZazcPhW5Y" />

```

![](https://unsplash.com/photos/JoZazcPhW5Y)

Can you please help?

---

<div class="post-metadata">

### Author: ![FunnySmile](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/funnysmile/32/229095_2.png) [@FunnySmile](https://meta.discourse.org/u/FunnySmile)
#### Post date: [July 25, 2022, 5:39am UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/6 "2022-07-25T05:39:03Z")

</div>

You may want to note that may cloud bed does not allow a third party web site calls, generally speaking, a part of the cloud returns the address of the bed, some are can call, but some not, they will generally give extra img tags special url, if not, may be the cloud bed does not allow a third party call, you can use the browser developer mode, Then select Network to see what errors are encountered with image loading.

---

<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: [August 8, 2022, 8:47am UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/7 "2022-08-08T08:47:31Z")

</div>

> [@Chittaranjan\_Jena](#):
>
> I am trying to post an image to post using the img tag like below but it’s not showing?

You can just add the url to the image without any html img tags

`https://unsplash.com/photos/JoZazcPhW5Y`

like this

> **[Photo by Keshaun Slevinsky on Unsplash](https://unsplash.com/photos/a-person-holding-a-camera-JoZazcPhW5Y)**
>
> Download this photo by Keshaun Slevinsky on Unsplash

and discourse will onebox it. But that isn’t even a link to an image, that just link to a page on the [unsplash.com](http://unsplash.com) website. You will need to use the actual link for the image:

`https://images.unsplash.com/photo-1658603458978-4a32b8cdadb7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3272&q=80`

 ![](https://global.discourse-cdn.com/meta/original/4X/d/5/7/d576ab639d23c7f98e3364a3a732941979acb16c.jpeg)

If you are using the api, you can include the image url in the ‘raw’ field of the post.

---

<div class="post-metadata">

### Author: ![Chittaranjan\_Jena](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chittaranjan_jena/32/260683_2.png) [@Chittaranjan\_Jena](https://meta.discourse.org/u/Chittaranjan_Jena)
#### Post date: [August 24, 2022, 1:20pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/8 "2022-08-24T13:20:59Z")

</div>

Hi @blake  
I tried as suggested. Here is my post request body

 ![Screenshot 2022-08-24 at 6.48.50 PM](https://global.discourse-cdn.com/meta/original/4X/d/b/9/db923d2c718a2e70a422b2b7675071c2dd7f862f.png)

When i saw the post, it looks like this (Just shows the link not the image). When i put the link in the browser, i can get the image

 ![Screenshot 2022-08-24 at 6.46.54 PM](https://global.discourse-cdn.com/meta/original/4X/6/5/d/65def172b0c67762f68b8b1e2ef92960e526c867.png)

Regards  
Chitta

---

<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: [August 24, 2022, 3:16pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/9 "2022-08-24T15:16:40Z")

</div>

Can you add the image url on it’s own line?

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/9/7/7972b5a8dd15338168657221b34cc3565bbe93cc.png)

```plaintext
asdfasdfasdf https://stayrightcon.com/wp-content/uploads/2020/05/Stayright-logo_resolution-72ppi.png

asdfasdf

https://stayrightcon.com/wp-content/uploads/2020/05/Stayright-logo_resolution-72ppi.png

```

---

<div class="post-metadata">

### Author: ![Simranjit\_Kaur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simranjit_kaur/32/295635_2.png) [@Simranjit\_Kaur](https://meta.discourse.org/u/Simranjit_Kaur)
#### Post date: [March 1, 2023, 5:39am UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/10 "2023-03-01T05:39:53Z")

</div>

I am also trying to find the same thing…post image in post api. Though i followed the step to upload the image and then get the url…but how can we send url in payload. Sending it in raw field post it in words…is there any way we can have image field as well in posts api? Please help

---

<div class="post-metadata">

### Author: ![FunnySmile](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/funnysmile/32/229095_2.png) [@FunnySmile](https://meta.discourse.org/u/FunnySmile)
#### Post date: [March 1, 2023, 1:08pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/11 "2023-03-01T13:08:52Z")

</div>

I’m sorry that I don’t understand your meaning at the moment. My follow-up solution is to use a third-party cloud image bed to upload the picture first and then return a picture url address.

```plaintext
<img src="url" />

```

---

<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: [March 1, 2023, 6:06pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/12 "2023-03-01T18:06:07Z")

</div>

> [@Simranjit\_Kaur](#):
>
> but how can we send url in payload. Sending it in raw field post it in words

Correct. Pass it in as a markdown link.

```plaintext
![image|581x455](upload://c4MB1Yzppr5Omv68MoJBQINqqiB.png)

```

> [@Simranjit\_Kaur](#):
>
> is there any way we can have image field as well in posts api?

Nope, because this is the same workflow that is used in the composer UI. You will have to make two api calls, one for the upload and the other to include the URL in post payload.

---

<div class="post-metadata">

### Author: ![Simranjit\_Kaur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simranjit_kaur/32/295635_2.png) [@Simranjit\_Kaur](https://meta.discourse.org/u/Simranjit_Kaur)
#### Post date: [March 6, 2023, 4:54am UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/13 "2023-03-06T04:54:50Z")

</div>

Yeah correct, This is how i implemented this… just pass  
“[image|581x455(upload://c4MB1Yzppr5Omv68MoJBQINqqiB.png)” in raw in post api. Here  
1)image-any name you want to give  
2)upload://c4MB1Yzppr5Omv68MoJBQINqqiB.png- this is the shorturl we get while uploading the image using upload api.

for steps: 1)first we have to upload the image using /upload.json api  
step 2) pass the short url in above format with other post content in ‘raw’ field.

Thank you for your reply @blake

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [April 29, 2023, 3:33pm UTC](https://meta.discourse.org/t/add-images-to-api-posts/204651/14 "2023-04-29T15:33:16Z")

</div>


