and more in a single search tool across platforms. Read the announcement here. |
01/17/2024 04:19 AM - last edited on 01/17/2024 05:35 AM by Sunil
Hi,
I am trying to use the API to bulk upload users via a csv file. I am using the following example as stated in the docs https://documenter.getpostman.com/view/23973797/2s9YC5xBrz#a6d2f398-08cb-4a62-b839-c6640a5ceff1
Here's my code:
import requests
import json
import sys
from utils.config import base_url
from utils.auth import login
if __name__ == "__main__":
bearer = login()
if len(sys.argv) == 1:
print("provide csv file")
sys.exit(1)
file_path = sys.argv[1]
file_name = file_path.split("/")[-1]
url = f"{base_url}/uploadUserRequest"
payload = {
"delimiter": "comma",
}
files = [
(
"file",
(
file_name,
open(file_path, "rb"),
"text/csv",
),
)
]
headers = {"Authorization": f"Bearer {bearer}", "Content-Type": "application/json"}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response)
print(response.text)
And here's the response:
(.venv) λ /tests python -m tools.upload_csv_file.main /Users/oscar.melin/Downloads/users.csv
TERMINAL OUTPUT:
<Response [403]>
<!doctype html><html lang="en"><head><title>HTTP Status 403 – Forbidden</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 403 – Forbidden</h1><script defer src="https://static.cloudflareinsights.com/beacon.min.js/v84a3a4012de94ce1a686ba8c167c359c1696973893317" integrity="sha512-euoFGowhlaLqXsPWQ48qSkBSCFs3DPRyiwVu3FjR96cMPx+Fr+gpWRhIafcHwqwCqWS42RZhIudOvEI+Ckf6MA==" data-cf-beacon='{"rayId":"846e79fdbea39920","b":1,"version":"2024.1.0","token":"88a9664171984c7baa70eaf379c7666f"}' crossorigin="anonymous"></script>
</body></html>
What am I doing wrong?
I found one issue: The url should be:
# base_url in this case is:
# https://my-organisation.saviyntcloud.com/ECM/api
url = f"{base_url}/v5/uploadUserRequest"
But now i'm getting the following response:
TERMINAL OUTPUT:
>>> <Response [412]>
>>> {"msg":"file cannot be null or blank","errorcode":"1"}
And I can verify that the file is not null or blank.
[This message has been edited by moderator to merge reply comment]
Solved! Go to Solution.
01/17/2024 05:40 AM - edited 01/17/2024 05:43 AM
It's me again. I have managed to get it working. I removed the is part:
PREVIOUS:
headers = {
'Authorization': 'Bearer {{token}}',
'Content-Type': 'application/json'
}
SOLUTION:
headers = {
'Authorization': 'Bearer {{token}}'
}
# i.e., remove the "Content-Type": "application/json"
Here's the final code:
import requests
import sys
from utils.config import base_url
from utils.auth import login
if __name__ == "__main__":
bearer = login()
if len(sys.argv) == 1:
print("You need to provide csv file")
print("Usage:")
print("\tpython -m tools.upload_csv_file.main /path/to/file.csv")
sys.exit(1)
file_path = sys.argv[1]
file_name = file_path.split("/")[-1]
url = f"{base_url}/v5/uploadUserRequest"
payload = {
"delimiter": "comma",
"zeroDayProvisioning": "no",
"generateSystemUsername": "yes",
"generateEmail": "no",
"checkrules": "yes",
}
with open(file_path, "rb") as f:
data = f.read()
files = [
(
"file",
(
file_name,
data,
"text/csv",
),
)
]
headers = {"Authorization": f"Bearer {bearer}"}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response)
print(response.text)
The user is successfully uploaded to Saviynt. However, I am still getting an error as response:
TERMINAL OUTPUT:
>>> <Response [500]>
>>> {"msg":"error occurred while uploading users","errorcode":"1"}
01/17/2024 05:42 AM
Which language of code ?
01/17/2024 05:44 AM
The language is Python (version 3.9).
01/19/2024 05:28 AM - edited 01/19/2024 05:29 AM
The language of code does not matter. Whether it is Java, C++ or Python, a HTTP request is still a HTTP request...