How to upload documents using RestAPI?

I want to upload document using api.I used this python api request.

    def create_test_document(session, abs_file_path):
        encoded_string = ''
        with open(abs_file_path, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read())
            print(encoded_string)
        raw_data = {
            'session': session,
            'module_name': 'Documents',
            'name_value_list': [
             {
                'name': 'filename',
                'value': encoded_string.decode('utf-8')
             },
             {
                'name': 'document_name',
                'value': 'Document test'
             }               
       ]}
        args = {
            'method': 'set_entry',
            'input_type': 'json',
            'response_type': 'json',
            'rest_data': json.dumps(raw_data)}
        response = requests.post(url=CRM_URL, data=args)
        print(f'RESPONSE FROM DOCUMENT CREATION')
        print(f'STATUS_CODE: {response.status_code}')
        print(f'JSON_BODY: {response.json()}')
      return response.json()

Firstly, welcome to the community. I presume that you’re (planning on) using the V8 Api?

From your example, I can’t see whether you have already configured your client and received your OAuth token? Once you’ve done that, then you can start using the rest interface.

Can you give us an idea as to exactly how far you’ve got with the setup and test process. What error are you getting and where is it going wrong for you?

I find that an excellent reference on how to get the best possible support for errors/bugs/problems is by following the guidelines outlined here:

https://www.chiark.greenend.org.uk/~sgtatham/bugs.html

I encourage everyone to share this link, and if anyone needs it in another language, then i’m game to sponsor the translation process! :grin:

Thanks…
Actually I am using API v4.1 Rest.
And I used login,set_entry,set_relationship etc methods.But I have doubts about uploading documents…

Firstly, I know that the interface version API v4.1 has been updated substantially in the latest v8 API.

wrt to API v4.1:

There is:

set_note_attachment
that might help.

You could also try get_module fields for the Documents module? That might provide a bit more insight into what data you need to provide (and the correct names for those fields)?

I’d expect that you’d use the Documents module to upload the document, and then use the set_relationship to point to the document that you’ve uploaded?

…or…perhaps try moving to the v8 API? :grinning:

Unfortunately, I’ve only played with the v8 API

Is there a download files method available with API? It should give us something as we authenticate user and pass session id so a genuine Access to a Document/File/Image should be there to allow admins to abstract some data outside CRM framework.

It was discussed below but no solution yet.