[英]Create Item in Dynamics 365 Business Central using Rest API
我正在嘗試使用 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.'
我嘗試在 postman(基本身份驗證)中創建項目,具有相同的 URL 並且請求正文和錯誤是:
{
"error": {
"code": "BadRequest",
"message": "Does not support untyped value in non-open type. CorrelationId: 4bc23d7b-f6b3-4eca-ab62-6fb7d37e23ac."
}
}
重要的是要注意,當我從請求正文中排除baseUnitOfMeasure
和itemCategory
屬性時,項目成功創建。 但是包括這些屬性會導致錯誤。 當我針對上述問題進行研究時,我從不同的來源發現,當字段/屬性輸入錯誤時會出現此類問題。 如上所述,我正在從 Microsoft 文檔復制請求正文,所以我不知道是哪個字段導致了問題。 請幫我解決這個問題。 謝謝
對於未來的訪客/讀者:
花了一些時間讓我知道可以在 Microsoft Document中找到的請求正文實際上包含一些錯誤的屬性名稱。 我們可以從 go 到https://api.businesscentral.dynamics.com/v1.0/[您的域]/api/v1.0/$metadata 在身份驗證后我們得到 Z3501BB093D363810B67105ED 的屬性名稱。 所以,我發現工作的請求正文是:
{
"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": ""
}
BadRequest 通常是錯誤的數據類型或請求正文中的 json 不是服務器端所期望的。 我不建議在請求正文中添加任何 null 字段,這也會導致錯誤請求。
我的問題是通過 rest api 更新項目卡上的字段重量或.net_weight 在哪里? 您是否必須構建一個自定義的 api 才能在表中包含一個在 api 中不可見的字段以進行更新?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.