REST API: Upload assets from python client

Hey there,

i am working on some scripts to automate setting up RocketChat during deployment. I am really happy to see the possibility to set assets being added to the REST API. However the docs are not really clear and so far i did not succeed to actually upload an asset and set it.

Based on code i found on github (https://github.com/jadolg/rocketchat_API/tree/master/rocketchat_API Thanks!) i wrote a minimal wrapper around the REST API only implementing the endpoints that i need.

Logging in etc. all works fine and also a bunch of other methods i implemented already work fine (to set up channels and other settings).

So now i want to set up the logo and favicons. Here is the code to the post method i am using:

def post(self, method, files=None, use_json=True, **kwargs):
    reduced_args = self.__reduce_kwargs(kwargs)
    # Since pass is a reserved word in Python it has to be injected on the request dict
    # Some methods use pass (users.register) and others password (users.create)
    if 'password' in reduced_args and method != 'users.create':
        reduced_args['pass'] = reduced_args['password']
    if use_json:
        return requests.post(
            self.server_url + self.API_path + method,
            json=reduced_args,
            files=files,
            headers=self.headers,
            verify=self.ssl_verify)
    else:
        return requests.post(
            self.server_url + self.API_path + method,
            data=reduced_args,
            files=files,
            headers=self.headers,
            verify=self.ssl_verify)

And here is the code i played with to upload and set the logo:

def set_asset(self):
    response = self.post(
        "assets.setAsset",
        use_json=False,
        files={
            'logo.png':
            open(r"C:\Users\tkaufmann\Pictures\rocketeer.png", 'rb')
        },
        logo="logo.png")
    return response

I tried various combinations and settings but i am getting error code 400 (bad request) no matter what i try. I read the docs but i am unsure how to get this to work. Any help would be greatly appreciated.

Cheers,
Thorsten

Hi @instinct-vfx, could you please provide us the complete error response? Because there is probably an error message within your error response. Apparently your request is okay, I’m not a python expert but I think that is okay. The request should be sent through form-data (it’s your case), then the complete error response will help us.
Thanks.

Thanks a lot @marcos.defendi . The full response is as follows:

{u’errorType’: u’error-invalid-asset’, u’success’: False, u’error’: u’Invalid asset [error-invalid-asset]’}

Thanks @instinct-vfx, I checked the codebase and the error that you received and I noticed that probably the error is caused by wrong name of the asset. As you can see here, and here, the only two ways to throw the error that you received. Then the asset name when you attached in the 'form-data` request must be one of these. As I said before, I’m not a python expert, but probably the name of the asset that’s wrong.

1 Like