Hi all,
In Postman I need to use/call REST API from the version BC365 BC18 Cu2.
I want to add an attachment to the GL Journal Line.
All works fine until I try to upload a file.
1/ I create the record (Incoming Documents) by calling
https://localhost:18348/BC180/api/v2.0/companies(e03e92aa-f0c7-eb11-bb69-000d3a21ebdb)/attachments
and body
{
"parentId" : "ccd09dda-f0c7-eb11-bb69-000d3a21ebdb",
"fileName": "C:\\Temp\\ANAV.pdf",
"parentType": "Journal"
}
the response body is which means all is good
{
"
@odata.context": "
https://localhost:18348/BC180/api/v2.0/$metadata#companies(e03e92aa-f0c7-eb11-bb69-000d3a21ebdb)/attachments/$entity",
"
@odata.etag": "W/\"JzQ0O1I5ay9wMWVlTTJ4QkpHNWphZmRneWU0WU5sbEhlNXFBLzVmdGl5VmRuT0U9MTswMDsn\"",
"id": "4ad5f039-6da1-ec11-afa1-00090faa0001",
"parentId": "ccd09dda-f0c7-eb11-bb69-000d3a21ebdb",
"fileName": "C:\\Temp\\ANAV.pdf",
"byteSize": 0,
"attachmentContent@odata.mediaEditLink": "
https://localhost:18348/BC180/api/v2.0/companies(e03e92aa-f0c7-eb11-bb69-000d3a21ebdb)/attachments(4ad5f039-6da1-ec11-afa1-00090faa0001)/attachmentContent",
"attachmentContent@odata.mediaReadLink": "
https://localhost:18348/BC180/api/v2.0/companies(e03e92aa-f0c7-eb11-bb69-000d3a21ebdb)/attachments(4ad5f039-6da1-ec11-afa1-00090faa0001)/attachmentContent",
"lastModifiedDateTime": "2022-03-11T18:58:25Z",
"parentType": "Journal"
}
2/ I then try to upload the pdf file by using the
@odata.etag in the header and calling the
https://localhost:18348/BC180/api/v2.0/companies(e03e92aa-f0c7-eb11-bb69-000d3a21ebdb)/attachments(4ad5f039-6da1-ec11-afa1-00090faa0001)/attachmentContent
In the Body I tried to pass Base64 as raw (json, or just the base64 string) and binary, I think I tried all options in postman
but I always get the error message
{
"error": {
"code": "Unknown",
"message": "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. CorrelationId: 7ef17846-8731-4716-a79c-7d7d2e436534."
}
}
Does anyone know how to upload the file by using the REST API call above ?
by the way the Documentation regarding this
https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/api-reference/v2.0/api/dynamics_attachment_update
is wrong as it is a copy from api1.0
I also tried api1.0 but I am getting the same error message.
Thanks for your help.
Answers
The problem was in the etag and If-Match tag in the header
snip---
HEADERS:
If-Match * (you can also use the eTag: "W/\"JzQ0O1I5ay9wMWVlTTJ4QkpHNWphZmRneWU0WU5sbEhlNXFBLzVmdGl5VmRuT0U9MTswMDsn\"",
---snip---
If you use the etag
"JzQ0O1I5ay9wMWVlTTJ4QkpHNWphZmRneWU0WU5sbEhlNXFBLzVmdGl5VmRuT0U9MTswMDsn\"
you will get the error message "The input is not a valid Base-64 string as it contains a non-base 64 character, ..."
If you use
"JzQ0O1I5ay9wMWVlTTJ4QkpHNWphZmRneWU0WU5sbEhlNXFBLzVmdGl5VmRuT0U9MTswMDsn"
then you can upload the file only once after that you will get the error
"Another user has already changed the record. CorrelationId: 4b20e80f-1924-4465-8fd0-5c9bd84ec140."
So the best option is to use
If-Match *
in the header as this works fine.