我正在尝试使用 Microsoft Dynamics Business Central Rest API,以使用以下端点创建项目:
https://api.businesscentral.dynamics.com/v1.0/mydomain.com/api/v1.0/companies({id})/items
以下是我的代码:
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();
以下是请求正文(从此处复制):
{
"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"
}
当我尝试执行代码时,endpointRequest.GetResponse();
出现以下错误:
The remote server returned an error: (400) Bad Request.'
我尝试在邮递员(基本身份验证)中创建项目,具有相同的 URL 和请求正文,错误是:
{
"error": {
"code": "BadRequest",
"message": "Does not support untyped value in non-open type. CorrelationId: 4bc23d7b-f6b3-4eca-ab62-6fb7d37e23ac."
}
}
重要的是要注意,当我从请求正文中排除baseUnitOfMeasure
和itemCategory
属性时,项目成功创建。但是包括这些属性会导致错误。正如我对上述问题的研究,我从不同的来源发现,当字段/属性输入错误时会出现此类问题。如上所述,我正在从 Microsoft 文档中复制请求正文,因此我不知道是哪个字段导致了问题。请帮我解决这个问题。谢谢