'JSONDecodeError while trying to post csv value via python's requests.put method
I am currently working in python's requests library and accessing Salesforce API. I have successfully
- Accessed the access_token from Salesforce API
- Obtained the Session_ID
Now I need to do a upsert operation in salesforce using requests.put in text/csv format (as requested by the API developer)
Please see below for the code snippet and I have not shown the above two steps of the code
# Data for upsert operation
data = {
"Name":["ABC"],
"Model_Score__c":['Low'],
"Email__c":['Y'],
"Call__c":['N'],
"Processing_Date__c":['2022-02-24']
}
dfData = pd.DataFrame(data)
dfData_csv = dfData.to_csv(index=False, encoding='utf8')
# Headers to be sent with the put request
header = {}
header['Authorization'] = 'Bearer xxxxxx...xxxx'
header['Content-Type']='text/csv'
header['Connection'] = 'keep-alive'
header['Accept-Encoding'] = 'gzip, deflate, br'
# Points to the URL where we need to perform the operation and test_api_url is a sandbox url
put_url = test_api_url+f'/{session_id}/batches'
# Calling the put request
put_call_response = requests.put(put_url, data=dfData_csv, headers=header)
I get the following error
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Please note that I am able to test it successfully in Postman and please see below the data sent in the body of the postman put request. I have also sent the raw text as shown below using python but I still get the same error
"Name",Model_Score__c,Email__c,Call__c,Processing_Date__c
"ABC",Low,Y,N,2022-02-24
Any help is much appreciated.
Solution 1:[1]
It appears that you are trying to use the Bulk API (the REST API does not accept CSV data), but you're calling the wrong endpoint and haven't performed any of the required setup to do so.
put_url = test_api_url+f'/{session_id}/batches'
Never, ever put your session id in a URL! In any case, it's not the right URL for the Bulk API.
If you're trying to work with the more recent Bulk API 2.0, you want to do an ingest operation. (Detail).
With Bulk API 1.0, you'd do a more complex sequence of creating a job, adding batches, closing the job and monitoring.
I recommend reading the entire Bulk API Developer Guide. These APIs are complex and tricky.
Solution 2:[2]
Actually my answer was correct. The reason why I was getting the error is because I was executing the below statement. My bad for not posting the below line of code in my question.
print(put_call_response.json())
The above print statement was throwing the error because I was trying to parse an object that is not of json type. If I check the status_code, I was getting 201 (which means successfully updated the record) and when I checked the backend, it was reflecting as well.
print(put_call_response.status_code)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | David Reed |
Solution 2 | IM80554Coding |