自定义Business Central API POST请求400错误排查与解决
解决Business Central API POST 400错误问题
我开发了一个Business Central API页面,向其发送POST请求时持续返回400错误。已先后解决CORS、401、404问题,确认CORS策略、认证方式、请求端点均正确,且数据表字段与JSON请求字段匹配。尝试过在请求中加入payloadid参数,但问题仍未解决,恳请指点解决方向。
API页面AL代码
page 50115 PunchoutReceiverAPI { PageType = API; APIPublisher = 'Contoso'; APIGroup = 'Punchout'; APIVersion = 'v2.0'; ApplicationArea = All; Caption = 'Punchout Receiver API'; EntityName = 'PunchoutReceiver'; EntitySetName = 'PunchoutReceivers'; EntityCaption = 'Punchout Receiver'; EntitySetCaption = 'Punchout Receivers'; SourceTable = PunchoutReceiver; DelayedInsert = true; InsertAllowed = true; layout { area(content) { repeater(Group) { field(Payload; Rec.Payload) { Caption = 'Payload'; } } } } actions { } }
待发送的cXML消息
"Payload": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE cXML SYSTEM \"http://xml.cxml.org/schemas/cXML/1.2.014/cXML.dtd\"> <cXML xml:lang=\"en-US\" payloadID=\"933695160894\" timestamp=\"2025-06-30T19:55:33.520Z\"> <Header> <From> <Credential domain=\"DUNS\"> <Identity>83528721</Identity> </Credential> </From> <To> <Credential domain=\"DUNS\"> <Identity>65652314</Identity> </Credential> </To> <Sender> <Credential domain=\"workchairs.com\"> <Identity>website 1</Identity> </Credential> <UserAgent>Zilch cXML application</UserAgent> </Sender> </Header> <Message> <PunchOutOrderMessage> <BuyerCookie>E281C149319B44BE96115813B382676B</BuyerCookie> <PunchOutOrderMessageHeader operationAllowed=\"edit\"> <Total> <Money currency=\"USD\">140.00</Money> </Total> </PunchOutOrderMessageHeader> <ItemIn quantity=\"2\"> <ItemID> <SupplierPartID>1</SupplierPartID> </ItemID> <ItemDetail> <UnitPrice> <Money currency=\"USD\">10.00</Money> </UnitPrice> <Description xml:lang=\"en\">Product 1</Description> <UnitOfMeasure>EA</UnitOfMeasure> </ItemDetail> </ItemIn> <ItemIn quantity=\"2\"> <ItemID> <SupplierPartID>3</SupplierPartID> </ItemID> <ItemDetail> <UnitPrice> <Money currency=\"USD\">30.00</Money> </UnitPrice> <Description xml:lang=\"en\">Product 3</Description> <UnitOfMeasure>EA</UnitOfMeasure> </ItemDetail> </ItemIn> <ItemIn quantity=\"3\"> <ItemID> <SupplierPartID>2</SupplierPartID> </ItemID> <ItemDetail> <UnitPrice> <Money currency=\"USD\">20.00</Money> </UnitPrice> <Description xml:lang=\"en\">Product 2</Description> <UnitOfMeasure>EA</UnitOfMeasure> </ItemDetail> </ItemIn> </PunchOutOrderMessage> </Message> </cXML>"
Business Central数据表AL代码
table 50102 PunchoutReceiver { DataClassification = ToBeClassified; fields { field(1; payloadid; Integer) { DataClassification = ToBeClassified; AutoIncrement = true; InitValue = 1; } field(2; Payload; Text[2048]) { DataClassification = ToBeClassified; } } keys { key(PK; payloadid) { Clustered = true; } } }
Flask后端POST请求代码
external_payload = { "Payload": "this is the data" } external_url = "https://api.businesscentral.dynamics.com/v2.0/******************/api/Contoso/Punchout/v2.0/companies(*****************)/PunchoutReceiver" access_token = get_BC_token() response = requests.post( external_url, json=external_payload, headers={ "Authorization": f"Bearer {access_token}", 'Content-Type': 'application/json' }, timeout=30 )
排查方向建议
- 字段长度超限:数据表中
Payload字段定义为Text[2048],但待发送的cXML内容长度远超2048字符,插入时会触发校验错误。建议将字段类型改为Blob或Text[10000](根据实际需求调整最大长度)。 - API端点路径错误:API页面定义的
EntitySetName是复数PunchoutReceivers,但请求URL末尾用的是单数PunchoutReceiver,这会导致路由不匹配。正确URL应结尾为/PunchoutReceivers。 - JSON格式有效性:待发送的cXML字符串包含大量双引号,需确保转义正确。虽然
requests的json参数会自动处理转义,但要检查原始字符串的转义符是否规范(比如Python中用\"转义双引号)。 - 查看详细错误响应:打印
response.text或response.json(),Business Central的400错误响应体通常会返回具体原因(如字段超限、必填项缺失等),这是最直接的排查依据。 - DelayedInsert设置影响:API页面启用了
DelayedInsert = true,会延迟数据插入,若请求隐含缺少必填字段可能导致提交失败。可暂时关闭该设置测试是否能正常插入。
内容的提问来源于stack exchange,提问作者Liam White
相关产品推荐
相关产品推荐

