0

我正在尝试使用 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."
    }
}

重要的是要注意,当我从请求正文中排除baseUnitOfMeasureitemCategory属性时,项目成功创建。但是包括这些属性会导致错误。正如我对上述问题的研究,我从不同的来源发现,当字段/属性输入错误时会出现此类问题。如上所述,我正在从 Microsoft 文档中复制请求正文,因此我不知道是哪个字段导致了问题。请帮我解决这个问题。谢谢

4

2 回答 2

0

BadRequest 通常是错误的数据类型,或者请求正文中的 json 不是服务器端所期望的。我不建议在请求正文中添加任何为空的字段,这也会导致错误的请求。

我的问题是要通过 rest api 更新的项目卡上的字段 weight 或 net_weight 在哪里?您是否必须构建一个自定义 api 只是为了在要更新的 api 中不可见的表中包含一个字段?

于 2022-02-16T03:21:21.283 回答
0

对于未来的访客/读者:

花了一些时间让我知道可以在 Microsoft Document中找到的请求正文实际上包含一些错误的属性名称。我们可以访问https://api.businesscentral.dynamics.com/v1.0/[your domain]/api/v1.0/$metadata,在验证后我们会得到 XML 来指定应该使用的属性名称。所以,我发现工作的请求正文是:

{
  "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": ""
}
于 2020-06-19T13:57:56.883 回答