# איך לקבל את כתובת ה-URL של התמונה שהועלתה באמצעות API של Discourse?

**URL:** https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800
**Category:** Development
**Tags:** rest-api
**Created:** [12 בינואר,‏ 2016,‏ 3:51am UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800 "2016-01-12T03:51:27Z")
**Posts on this page:** 10
**Page:** 2

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [16 בינואר,‏ 2017,‏ 3:25pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/22 "2017-01-16T15:25:43Z")

</div>

You can only do a synchronous upload as a staff user, see [Image upload with synchronous true for new users](https://meta.discourse.org/t/image-upload-with-synchronous-true-for-new-users/52886) for details.

Some more information

> if you want the url back, you will have to listen to the “/uploads/\*” channels via the Message Bus.

[https://meta.discourse.org/t/using-the-discourse-api-to-post-with-uploaded-files/29833/11](https://meta.discourse.org/t/using-the-discourse-api-to-post-with-uploaded-files/29833/11)

---

<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: [16 בינואר,‏ 2017,‏ 4:09pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/23 "2017-01-16T16:09:33Z")

</div>

I’m pretty sure since you aren’t using an API key you will have to be a staff user. Is your user a staff user?

---

<div class="post-metadata">

### Author: ![stefanm](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@stefanm](https://meta.discourse.org/u/stefanm)
#### Post date: [16 בינואר,‏ 2017,‏ 4:15pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/24 "2017-01-16T16:15:00Z")

</div>

no i am not, but i can be 🙂  
i will do my posts with stuff user, and problem solved.

but still, if the image is uploaded even without stuff user, what’s the difference?  
i am able to upload it, but just can’t see the url.

thanks a lot guys.

---

<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: [16 בינואר,‏ 2017,‏ 4:22pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/25 "2017-01-16T16:22:37Z")

</div>

If you are a staff user and you do syncrounous it should return the image url and id

---

<div class="post-metadata">

### Author: ![stefanm](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@stefanm](https://meta.discourse.org/u/stefanm)
#### Post date: [24 בינואר,‏ 2017,‏ 10:32am UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/26 "2017-01-24T10:32:38Z")

</div>

i did a call with api\_key and username and i easily get url to the image.  
but unfortunately i don’t have api\_key in all the cases, so i needed to fall back to message-bus.

can you explain me how to get url in the message-bus/poll request  
i did almost everything

message-bus/{client\_id}/poll  
with the same client\_id, i made upload of image and got success : ok  
i’ve sent ‘/uploads/composer’:0, ‘/uploads/composer’:{current\_message\_id}, again no luck

i’ve seen also in the code, inside upload controller  
something like this MessageBus.publish(“/uploads/#{type}”, data.as\_json, client\_ids: [client\_id])

i tried also with type, again no luck.

can you just explain what params i need to send for message-bus/poll  
so i can get url of image in the response?

thanks a lot!

---

<div class="post-metadata">

### Author: ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)
#### Post date: [26 בינואר,‏ 2017,‏ 6:28pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/27 "2017-01-26T18:28:50Z")

</div>

Is there any complete answer found to get upload URL with ajax?  
We are also interested to get one.  
many thanks.

---

<div class="post-metadata">

### Author: ![Denis\_Didkovsky](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Denis\_Didkovsky](https://meta.discourse.org/u/Denis_Didkovsky)
#### Post date: [27 במרץ,‏ 2017,‏ 11:58am UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/28 "2017-03-27T11:58:19Z")

</div>

Sorry for recalling rather old topic but we appreciate any support on getting uploaded url for non-staff user right after upload. many thanks.

---

<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: [5 בדצמבר,‏ 2017,‏ 12:14am UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/29 "2017-12-05T00:14:00Z")

</div>

> [@Denis\_Didkovsky](#):
>
> Is there any complete answer found to get upload URL with ajax?

> [@Denis\_Didkovsky](#):
>
> getting uploaded url for non-staff user right after upload

Here is a complete ajax file upload example which I verified works with non-staff users:

```plaintext
<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <script
        src="https://code.jquery.com/jquery-3.2.1.min.js"
        integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        crossorigin="anonymous">
    </script>
    <script>
      var api_username = '788336e9f81d21b4fa04';
      var api_key = '8f23cba0eec7cff2a995a8271589f5096d9b448d29af5d2e93383e43ac6e0f9b';
      function createPostWithUpload(url, width, height) {
        var data = {
          topic_id: "18",
          raw: '<img src=' + url + ' width=' + width + ' height=' + height + '>',
          api_username: api_username,
          api_key: api_key
        };
        $.ajax({
          type: "POST",
          url: "http://192.168.56.2:3000/posts",
          data: data,
          success: false,
          dataType: false
        });
      }
      function uploadFile() {
        var form = $('#uploadForm')[0];
        var formData = new FormData(form);
        formData.append('api_username', api_username);
        formData.append('api_key', api_key);
        formData.append('synchronous', 'true');
        formData.append('type', 'composer');
        var data = {
          'files[]': formData,
          type: "composer",
          synchronous: true,
          api_username: api_username,
          api_key: api_key
        };
        $.ajax({
          method: "POST",
          url: "http://192.168.56.2:3000/uploads.json",
          data: formData,
          processData: false,
          contentType: false,
          success: function(response) {
            console.log(response);
            var id = response.id;
            var url = response.url;
            var width = response.width;
            var height = response.height;
            createPostWithUpload(url, width, height);
          },
        });
      }
      function createPost() {
        var data = {
          topic_id: "18",
          raw: "my sample post that I'm making",
          api_username: api_username,
          api_key: api_key
        };
        $.ajax({
          type: "POST",
          url: "http://192.168.56.2:3000/posts",
          data: data,
          success: false,
          dataType: false
        });
      }
    </script>
  </head>
  <body>
    <form name="uploadForm" id="uploadForm">
      <p><input id="myFile" type="file" name="file"> </p>
      <p><input type="button" onClick="uploadFile();" value="Upload file"></p>
      </form>
      <input type="button" onClick="createPost();" value="Create Post"></p>
  </body>
</html>

```

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [7 בדצמבר,‏ 2017,‏ 7:45pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/30 "2017-12-07T19:45:53Z")

</div>

That code is seriously too bloated, with a formData structure inside an almost identical data structure.

But more important: there is no non-staff synchronous upload issue any more, the code was refactored last week.

[https://github.com/discourse/discourse/commit/eb428ef54dcc1d0d74f5d4a0d3fd12f1122dab65#diff-60328924b5c773b42aacabc0b5698af4](https://github.com/discourse/discourse/commit/eb428ef54dcc1d0d74f5d4a0d3fd12f1122dab65#diff-60328924b5c773b42aacabc0b5698af4)

---

<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: [8 ביוני,‏ 2024,‏ 12:43pm UTC](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800/31 "2024-06-08T12:43:49Z")

</div>

This topic was automatically closed after 3070 days. New replies are no longer allowed.

[Previous page](https://meta.discourse.org/t/how-to-get-uploaded-image-url-using-the-discourse-api/37800.md?page=1)
