How to specify a file in the 'filebody' in Python API post request?

388 Views Asked by At

I am trying to post a file to sendinblue using API in Python and struggling with what to put instead of "dataset1". It works if I manually type the file contents but not if I specify a file - I have tried csv, json, txt formats but nothing works. The error message says 'We could not find any valid email or SMS in the file you uploaded', I think it doesn't like the 'filebody' format.

My code below: Any help greatly appreciated!

import requests

url = "https://api.sendinblue.com/v3/contacts/import"

payload = {
    "listIds": [12],
    "emailBlacklist": False,
    "smsBlacklist": False,
    "updateExistingContacts": True,
    "emptyContactsAttributes": False,
    "fileBody": "dataset1"

headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "api-key": "APIKEY"
}

response = requests.request("POST", url, json=payload, headers=headers)


        
print(response.text)
2

There are 2 best solutions below

5
On

This should work. If not you can try some of these solutions.

files = {'upload_file': open('file.txt','rb')}
url = "https://api.sendinblue.com/v3/contacts/import"
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "api-key": "APIKEY"
}
response = requests.request("POST", url, files=files, headers=headers)
5
On

I think what you want is fileUrl instead of fileBody which can be referenced from here. You mentioned that if you type the contents manually, then it works fine. So, please try to use the url(which is publicly accessible) for fileUrl parameter in the request and let me know if it works for you!