MS Dynamics 365 Business Central. API POST, PATCH, DELETE does not work
The OData endpoint is different to the API endpoint.
I'd recommend using the API as it's the more up to date and efficient way to interact with data within Business Central.
The documentation for the API endpoint can be found here https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/, along with some examples such as updating customers, items, etc.
You can use OData to modify data, but only if the underlying object that drives the endpoint is a page (as opposed to a query), and must be marked with InsertAllowed. This is less optimised than the API endpoint. https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/use-odata-to-modify-data
The paramaters are as follows:
- base URL: the name of the server e.g. d365bc.vendor.com
- port: the port used for OData e.g. 7058
- serverinstance: the name of the Business Central server instance e.g. attain
- API publisher: used for grouping of custom API's (not required when using the standard API's)
- API group: used for grouping of custom API's (not required when using the standard API's)
- API version: used for grouping of custom API's (not required when using the standard API's)
The combination of base URL, port and server instance can be located under OData Services and the field OData Base URL on the Business Central service tier settings.
The base endpoint you need to use is (based on the information you supplied):
// Version 13
https://d365bc.vendor.com:7058/Attain/api/beta
// Version 14+
https://d365bc.vendor.com:7058/Attain/api/v1.0
To work with the API's you first need to get an id for the company - so you get the company list:
// Version 13
https://d365bc.vendor.com:7058/Attain/api/beta/companies
// Version 14+
https://d365bc.vendor.com:7058/Attain/api/v1.0/companies
With your company id you can then get a list of customers:
// Version 13
https://d365bc.vendor.com:7058/Attain/api/beta/companies([company id here!])/customers
// Version 14+
https://d365bc.vendor.com:7058/Attain/api/v1.0/companies([company id here!])/customers
You would use the same endpoint for POST or PATCH by providing the required data as json in the request body.
Requirements:
- API must be enabled on the Business Central server instance
- The user used for authentication must have a Web Service Access Key (created for the user in Business Central)
- The user must have the required permissions within Business Central for the relevant API's.
Comments:
- Some of the API's only support GET. This means you would get the 405 Not allowed error for POST or PATCH.
- You might need to add If-Match * to the request headers for PATCH operations.
Response to UPD.2:
Port 7057 and
https://d365bc.vendor.com:7057/Attain/WS/
is for SOAP Web Services and is not to be confused with API. You need the port 7058 as stated under the OData Services tab.To assign a Web Service Access Key you must navigate to Users in Business Central, find the user in question and click the button
Change Web Service Key
.
Some notes:
You need retrieve entities first (by make 'Get'), then when you need to do update/PUT or delete (beside the need to use correct method (PUT, DELETE)) You have to send in the Headers of your API call, the parameter: If-Match and make it equal to the value of odata.etag (that you got from the GET response), it is like this: If-Match : W/"JzQ0O0YxVTZnTlFpQ0NxbExDdmN0N2JGSDM2azN4dUhncEk1Nnl0MVhETGRIemc9MTswMDsn"
Regarding: https://d365bc.vendor.com:7058/attain/ODataV4/Company('{{company}}')/Customer(No='01121212') This is not OData: /Customer(No= you should use eq instead of the sign = However, it is better to use the ID (GUID) of the record and you must provide If-Match in header with value of odata.etag