Reputation: 1477
I am trying to consume Microsoft Dynamics Business Central Rest API, to create item using following endpoint:
https://api.businesscentral.dynamics.com/v1.0/mydomain.com/api/v1.0/companies({id})/items
Following is my code:
string requestBody = JsonConvert.SerializeObject(itemBodyValues);
string url = "https://api.businesscentral.dynamics.com/v1.0/mydomain.com/api/v1.0/companies({id})/items";
string encodedCredentials = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + WebServiceAccessKey));
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(url);
endpointRequest.ContentType = "application/json";
endpointRequest.Method = "POST";
//endpointRequest.Accept = "application/json;odata=verbose";
using (var streamWriter = new StreamWriter(endpointRequest.GetRequestStream()))
{
streamWriter.Write(requestBody);
}
endpointRequest.Headers.Add("Authorization", "Basic " + encodedCredentials);
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
Following is the request body (copied it from here):
{
"number": "1896-S",
"displayName": "ATHENS Desk",
"type": "Inventory",
"blocked": false,
"baseUnitOfMeasure": {
"unitCode": "PCS", //Unit of measure with this code exists in dynamics BC
"unitName": "Piece",
"symbol": "",
"unitConversion": null
},
"gtin": "",
"itemCategory": {
"categoryId": "TABLE", //Item category with this code exists in dynamics BC
"description": "Assorted Tables"
},
"inventory": 0,
"unitPrice": 1000.8,
"priceIncludesTax": false,
"unitCost": 780.7,
"taxGroupCode": "FURNITURE"
}
When i try to execute the code, then at endpointRequest.GetResponse();
i get the following error:
The remote server returned an error: (400) Bad Request.'
I tried creating item in postman (basic authentication), with same URL and request body and error is:
{
"error": {
"code": "BadRequest",
"message": "Does not support untyped value in non-open type. CorrelationId: 4bc23d7b-f6b3-4eca-ab62-6fb7d37e23ac."
}
}
It is important to note that item successfully gets created when i exclude baseUnitOfMeasure
and itemCategory
properties from request body. But including these properties causes the error. As i researched for the issue above, from different sources i found that, such issue appears when a field/property is mistyped. I am copying the request body from Microsoft document as mentioned above so i am clueless that which field is causing the issue. Please help me resolve this. Thanks
Upvotes: 0
Views: 3576
Reputation: 1
BadRequest is usually a bad datatype or json that is in the request body is not what is expected by the server side. I don't recommend adding any fields to the request body that are null this can also cause bad request.
My question is where is the field weight or net_weight on the item card to update via the rest api? Do you have to build a custom api just to include a field in the table not visible in the api to update?
Upvotes: 0
Reputation: 1477
For Future visitors/readers:
It took some R&D to let me know that the request body that can be found in Microsoft Document actually contains some wrong property name. we can go to https://api.businesscentral.dynamics.com/v1.0/[your domain]/api/v1.0/$metadata where after authentication we get the XML that specifies the property names that should be used. So, the request body that i found working is:
{
"number": "1836-S",
"displayName": "ATHENS Desk",
"type": "Inventory",
"blocked": false,
"baseUnitOfMeasure": {
"code": "PCS",
"displayName": "Piece",
"symbol": "",
"unitConversion": null
},
"gtin": "",
"itemCategory": {
"code": "TABLE", //make sure item category with this code doesn't already exists
"displayName": "Assorted Tables"
},
"inventory": 0,
"unitPrice": 1000.8,
"priceIncludesTax": false,
"unitCost": 780.7,
"taxGroupCode": ""
}
Upvotes: 0