I am having an issue accessing the API programmatically.
I am trying to send a POST request to send some data.
I was able to authenticate and send a GET request with no issues, but with the POST request I am getting an error: “The request is invalid.”
I am using the exact same data in both the Epicor REST Swagger UI, but when I run the POST request through Python or PowerShell it gives me the same error.
Inside the Epicor REST Swagger UI, I am making this POST request:
This posts fine and adds the information.
Then, I have the following code written in PowerShell and Python, neither are working:
PowerShell:
$Bytes = [System.Text.Encoding]::Ascii.GetBytes($Text)
$EncodedText = [Convert]::ToBase64String($Bytes)
$header = @{Authorization="Basic $EncodedText"}
$data_params = @{
Company="SEI";
Key1="test";
XFileRefXFileName="";
XFileRefXFileDesc="string";
RowMod="A"
}
Invoke-RestMethod https://epicor10/EpicorERPTEST/api/v1/Ice.BO.AttachmentSvc/Attachments -Method Post -ContentType "application/json" -headers $header -body $data_params
Python:
input_params ={
"Company": "SEI",
"RelatedToSchemaName": "Erp",
"RelatedToFile": "DMRHead",
"Key1": "test",
"XFileRefXFileName": "",
"XFileRefXFileDesc": "string",
"RowMod": "A"
}
post = requests.post('https://epicor10/EpicorERPTEST/api/v1/Ice.BO.AttachmentSvc/Attachments',
data=input_params,
headers={'Authorization': f'Basic {base64_message}'},
verify=False)
I have used the base64 encryption in both languages to make the call, and it works for authentication, so I don’t think the issue is there.
The error I am getting in both cases is:
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"The request is invalid."
},"innererror":{
"message":"entity : An error has occurred.\r\n","type":"","stacktrace":""
}
}
I can replicate this error in the the Swagger REST UI by putting an incorrect value into one of the fields, and the error I get is this:
Response Body:
{
"odata.error": {
"code": "",
"message": {
"lang": "en-US",
"value": "The request is invalid."
},
"innererror": {
"message": "entity : An error has occurred.\r\n",
"type": "",
"stacktrace": ""
}
}
}
Response Code:
400
Response Headers:
{
"content-length": "221",
"content-type": "application/json; charset=utf-8",
"dataserviceversion": "3.0",
"date": "Tue, 09 Mar 2021 18:12:39 GMT",
"server": "Microsoft-IIS/10.0",
"x-powered-by": "ASP.NET"
}
I replicate this error in the UI by placing a string value into a Boolean value instead, where it was expecting ‘true’ or ‘false’ I passed ‘asdf’ and it throws this error.
However in my code I am not passing any improper values, and can pass the exact same parameters I am using in the code in the Swagger UI, and it works.
Is there a step I am missing, or something that needs to be included when using the API programmatically?
I appreciate any help!