Announcement

Collapse
No announcement yet.

API issue while uploading attachment

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API issue while uploading attachment

    I am having an issue that has me at a complete roadblock. I am writing a python script to upload records to espocrm through the API. one part of this script is to upload a .pdf file. I have been able to upload the file, however, when I view it in espocrm the .pdf file is blank. I believe it has something to do with the encoding. Any help would be great. Below is the part of the script that I am having the difficulty with.

    Code:
    binary_file = open('data/GetReport.pdf', 'rb')
    
    binary_file_data = binary_file.read()
    
    base64_encoded_data = base64.b64encode(binary_file_data)
    
    dataPush = {
    
    "name": "GetReport.pdf",
    
    "type": 'application/pdf',
    
    "role": "Attachment",
    
    "relatedType": "EventIndex",
    
    "field": "document",
    
    "file": f'data:application/pdf;base64,{base64_encoded_data}'
    
    }
    
    
    push = client.request('POST', 'Attachment', dataPush)
    
    record = '607e2084e00333052'
    
    doc_id = push['id']
    
    print(doc_id)
    
    doc_name = push['name']
    
    print(doc_name, '\n')
    
    dataPush = {
    
    'documentName': doc_name,
    
    'documentId': doc_id,
    
    }
    
    pushDoc = client.request('PUT', 'EventIndex/' + record, dataPush)

  • #2
    Ok, I got it fixed. The following the correct way in python:


    from espo_api_client import EspoAPI
    import base64
    binary_file = open('data/GetReport.pdf', 'rb')
    binary_file_data = binary_file.read()
    base64_encoded_data = base64.b64encode(binary_file_data)
    dataPush = {
    "name": "GetReport.pdf",
    "type": 'application/pdf',
    "role": "Attachment",
    "relatedType": "EventIndex",
    "field": "document",
    "file": (f'data:application/pdf;base64,{base64_encoded_data.decode()}'),
    }
    push = client.request('POST', 'Attachment', dataPush)

    Comment

    Working...
    X