# \[SOLVED\] Uploading an image with api v. 4 not working

**URL:** <https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491>\
**Category:** Troubleshooting\
**Created:** [February 9, 2018, 6:39am UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491 "2018-02-09T06:39:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ParkerMc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/parkermc/32/1875_2.png) [@ParkerMc](https://forum.mattermost.com/u/ParkerMc)\
**Post date:** [February 9, 2018, 6:39am UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/1 "2018-02-09T06:39:16Z")

</div>

#### Summary

Uploading an image with API v. 4 not working.  
There is no preview and no perms for other users in the channel other than admins.

#### Steps to reproduce

Version: 4.6.0  
Snipit of Code: [https://pastebin.com/p0nmizb3](https://pastebin.com/p0nmizb3)  
1: Upload image via api  
2: Bind a post to the image via api  
3: Look at it

#### Expected behavior

It should let me download on any account that is in the channel I put it in and have a preview as normal.

#### Observed behavior

`{"id":"api.context.permissions.app_error","message":"You do not have the appropriate permissions","detailed_error":"","request_id":"mrdmgqk6ejbc5dhjra9c7icy9e","status_code":403}`

This line is in the log: `[2018/02/09 00:01:32 CST] [EROR] /api/v4/files/nbur81htzbnedqak3wfqjfqueh:Permissions code=403 rid=mrdmgqk6ejbc5dhjra9c7icy9e uid=pfc5hne4pfdnudz97bpnz1f31r ip=::1 You do not have the appropriate permissions [details: userId=pfc5hne4pfdnudz97bpnz1f31r, permission=read_channel]`

- There is no preview no matter what accout
- You can download it on an admin accout
- You can download it on the account it was uploaded though
- You can’t download it on any other account
- The image is in the right channel folder in the file system(got channel id from api while uploading also checked database)

No preview  
 ![image](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex020/uploads/mattermost/original/2X/8/8b4a0b3cb5b31e150160a75c936965dfa0660cc8.png)

#### Stuff I have tried

- Clearing the cache
- Restarting mattermost
- Setting the post id in the database(and clearing the cache afterward)

The row for the file in FileInfo: [https://pastebin.com/KkdQzrRK](https://pastebin.com/KkdQzrRK)

---

<div class="post-metadata">

**Author:** ![lindy65](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/lindy65/32/1811_2.png) [@lindy65](https://forum.mattermost.com/u/lindy65)\
**Post date:** [February 11, 2018, 6:06pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/2 "2018-02-11T18:06:29Z")

</div>

Linking to [the conversation](https://pre-release.mattermost.com/core/pl/9r3b6tigxbbrpmhpqdwkrwx44a) on our nightly build server so we don’t duplicate responses…

---

<div class="post-metadata">

**Author:** ![hmhealey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/hmhealey/32/1915_2.png) [@hmhealey](https://forum.mattermost.com/u/hmhealey)\
**Post date:** [February 12, 2018, 2:12pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/3 "2018-02-12T14:12:58Z")

</div>

Hi @ParkerMc,

I have a couple questions:

1. Is the post being created before you upload the file? The steps that the web app uses are to
  1. Upload the file
  2. Create the post, including the uploaded file as part of the `file_ids` field of the post

2. Are you sure that the file is being uploaded for the same team and channel as the post appears in? I see that the file has a channel ID of `wtmhorhhfjyppespk6qidexjzh` and no team ID, so does that match the post?

---

<div class="post-metadata">

**Author:** ![ParkerMc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/parkermc/32/1875_2.png) [@ParkerMc](https://forum.mattermost.com/u/ParkerMc)\
**Post date:** [February 12, 2018, 7:01pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/4 "2018-02-12T19:01:52Z")

</div>

@hmhealey,

1. Yes, the post is being created before I upload the file. We are looking to switch from rocket.chat to mattermost so a make a program that reads the data from mongo, and imports everything but the files via the bulk loading command.

2. This error happens in all channels I have tried. In this case it was in a dm, same channel id. Here is the row if that helps at all. [https://pastebin.com/3cKycYVB](https://pastebin.com/3cKycYVB)

Thanks!

---

<div class="post-metadata">

**Author:** ![hmhealey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/hmhealey/32/1915_2.png) [@hmhealey](https://forum.mattermost.com/u/hmhealey)\
**Post date:** [February 13, 2018, 2:50pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/5 "2018-02-13T14:50:55Z")

</div>

I don’t think we’ve ever tested uploading files after a post is made, but it should work in theory, and everything looks write with the Post and FileInfo objects. Maybe check to see if there’s a row in the ChannelMembers table with the `user_id` of the user you’re trying to view the file with and `channel_id = 'wtmhorhhfjyppespk6qidexjzh'`, and see what the `roles` field for that row is?

The error message would indicate that the user doesn’t even have the ability to read the channel ([code here](https://github.com/mattermost/mattermost-server/blob/master/api4/file.go#L118)) which would only reasonably happen if they weren’t in the channel to begin with

---

<div class="post-metadata">

**Author:** ![ParkerMc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/parkermc/32/1875_2.png) [@ParkerMc](https://forum.mattermost.com/u/ParkerMc)\
**Post date:** [February 13, 2018, 9:05pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/7 "2018-02-13T21:05:15Z")

</div>

While getting the post id again, I noticed that all of a sudden the file works, I did some tests to see what worked to fix it here is what I found out.

In the fileinfo part of the database doesn’t have PostId after the post is updated.  
So the workaround is to set the PostId manually(of course this would be a bad idea in a production server).

After that I can download the file from the api.  
To fix the preview I do have to log out clear my browser cache then log back in but that is partly expected.  
This work around I have tested on 3 files/posts. I assume there is a missing query to set the PostId when the file is updated.

---

<div class="post-metadata">

**Author:** ![ParkerMc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/parkermc/32/1875_2.png) [@ParkerMc](https://forum.mattermost.com/u/ParkerMc)\
**Post date:** [February 13, 2018, 9:23pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/8 "2018-02-13T21:23:26Z")

</div>

Yep, I think this code [https://github.com/mattermost/mattermost-server/blob/master/app/post.go#L182-L195](https://github.com/mattermost/mattermost-server/blob/master/app/post.go#L182-L195)  
Should also be here [https://github.com/mattermost/mattermost-server/blob/07fd7aeeb8eb2b198b01b713a4ab57f6352faef2/app/post.go#L404](https://github.com/mattermost/mattermost-server/blob/07fd7aeeb8eb2b198b01b713a4ab57f6352faef2/app/post.go#L404)

I haven’t worked much with go so that may not be the real fix.

---

<div class="post-metadata">

**Author:** ![hmhealey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/hmhealey/32/1915_2.png) [@hmhealey](https://forum.mattermost.com/u/hmhealey)\
**Post date:** [February 15, 2018, 2:34pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/9 "2018-02-15T14:34:32Z")

</div>

I think similar logic should work there, but I don’t know how well the UI and caching code would handle files being added to a post after it’s created since we’ve never tried that ourselves. If you’ve got the environment set up to build Mattermost, you could give it a shot though. We’d definitely be open to a PR that added this since I imagine you’re not the first to try migrating to Mattermost this way

---

<div class="post-metadata">

**Author:** ![ParkerMc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/parkermc/32/1875_2.png) [@ParkerMc](https://forum.mattermost.com/u/ParkerMc)\
**Post date:** [February 17, 2018, 9:33pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/10 "2018-02-17T21:33:02Z")

</div>

I’ll have to test it soon. My computer hard drive got corrupted on Wednesday so I’ll have to reinstall everything before I can test it.

---

<div class="post-metadata">

**Author:** ![ParkerMc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/parkermc/32/1875_2.png) [@ParkerMc](https://forum.mattermost.com/u/ParkerMc)\
**Post date:** [March 4, 2018, 6:18am UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/11 "2018-03-04T06:18:20Z")

</div>

Due to some other reasons with mattermost my group has decided against using mattermost. Here is the patched I ended up making. If anyone wants to use it feel free. From the testing I’ve done it works fine.  
[https://pastebin.com/CTByjTeS](https://pastebin.com/CTByjTeS)

---

<div class="post-metadata">

**Author:** ![lindy65](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/lindy65/32/1811_2.png) [@lindy65](https://forum.mattermost.com/u/lindy65)\
**Post date:** [March 5, 2018, 7:22am UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/12 "2018-03-05T07:22:23Z")

</div>

Hi @ParkerMc,

Thanks for the feedback 🙂

We’re always open to hearing why people make the decisions they do regarding use of Mattermost and we’d really appreciate hearing from you about why your team decided against our product.

Would you be open to joining our [nightly build server](https://pre-release.mattermost.com/signup_user_complete/?id=f1924a8db44ff3bb41c96424cdc20676) to share the feedback in the ~product-management channel?

---

<div class="post-metadata">

**Author:** ![fernandowiek](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fernandowiek](https://forum.mattermost.com/u/fernandowiek)\
**Post date:** [June 4, 2019, 2:04pm UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/13 "2019-06-04T14:04:52Z")

</div>

Hey, @ParkerMc.  
Could you share your solution to migrate rocket.chat to Mattermost? I want to do this to.  
Thank you!

---

<div class="post-metadata">

**Author:** ![ahmaddanial](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/ahmaddanial/32/2938_2.png) [@ahmaddanial](https://forum.mattermost.com/u/ahmaddanial)\
**Post date:** [April 17, 2020, 1:37am UTC](https://forum.mattermost.com/t/solved-uploading-an-image-with-api-v-4-not-working/4491/14 "2020-04-17T01:37:00Z")

</div>

Hello, @fernandowiek

May I know if you were able to complete the migration from rocket.chat to Mattermost? If yes, can you share the steps to do so?

At the same time, I would like to know if the article here helps - [Bringing Data from Bespoke Solutions into Mattermost](https://docs.mattermost.com/administration/migrating.html#bringing-data-from-bespoke-solutions-into-mattermost)?
