Forum Discussion

jeffshurtliff's avatar
5 years ago

Getting 403 response when creating message with attachment using API v2 and Python

When creating messages using the Community API v2 and the Python requests library, it works fine when not trying to upload an attachment, as shown in the sanitized excerpt below.     >>> headers ...
  • WillB's avatar
    WillB
    5 years ago

    jeffshurtliff I got it working with the following code: 

     

    import requests
    
    headers = {
        'li-api-session-key': 'XXXXXXXXXX_XXXXXXXXXXX_XXXXXXXXXXXX',
    }
    
    files = {
        'api.request': (None, '{\n"data":{\n"type":"message",\n"subject":"This is the message subject",\n"board":{\n"type":"board",\n"id":"KB1"\n},\n"attachments":{\n"list_item_type":"attachment",\n"items":[\n{\n"type":"attachment",\n"field":"attachment1",\n"filename":"pdf1.PDF"\n},\n{\n"type":"attachment",\n"field":"attachment2",\n"filename":"pdf2.PDF"\n}\n]\n}\n}\n}'),
        'attachment1': ('/Users/will.bowen/Documents/pdf1.PDF', open('/Users/will.bowen/Documents/pdf1.PDF', 'rb')),
        'attachment2': ('/Users/will.bowen/Documents/pdf2.PDF', open('/Users/will.bowen/Documents/pdf2.PDF', 'rb')),
    }
    
    response = requests.post('https://rsalink-stage.rsa.com/api/2.0/messages', headers=headers, files=files)