0

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)
Goldwave
  • 599
  • 3
  • 13
  • 3
    Does this answer your question? [python requests file upload](https://stackoverflow.com/questions/22567306/python-requests-file-upload) – Goldwave Oct 02 '20 at 15:11

2 Answers2

0

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)
Goldwave
  • 599
  • 3
  • 13
0

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!

rudy0807
  • 152
  • 1
  • 8
  • Thank you. Do you know if there is a way to convert a csv file into raw URL link in Python? (without going through Github and doing it manually) – Irina Obrazcova Oct 07 '20 at 08:56
  • I think you will need to save the csv file in github or any other cloud to make it accessible by Sendinblue’s servers. So, you will need to pass the Url to that file in your request. And once your contacts have been imported, you can remove the file. – rudy0807 Oct 07 '20 at 09:24
  • Yes with github the problem is I have to manually upload the file and convert it but I need this to be an automatic process. I have also tried to create a URL link from the OneDrive but that's not working either.. – Irina Obrazcova Oct 07 '20 at 15:46
  • How often do you plan on importing the contacts? – rudy0807 Oct 07 '20 at 15:48
  • If you don’t intent to do it very often, I will suggest you to create and upload the file manually. But in case you need to do it often, you can try using AWS S3, which should solve your purpose. You will need to create the csv as you are doing now and then save it on S3. Then, you can use that url for your body request. – rudy0807 Oct 07 '20 at 15:51