调用python-amazon-sp-api的createInboundPlan接口时遭遇参数验证错误,请求协助排查
看起来你在调用SP API的createInboundPlan接口时踩了个容易忽略的坑——明明提交的Payload里有完整数据,却被API返回一堆“字段为空/不符合格式”的验证错误,这确实挺让人头大的,我来帮你拆解下问题所在:
你的请求Payload(提交前的内容)
{ "body": { "destinationMarketplaces": [ "A1F83G8C2ARO7P" ], "items": [ { "labelOwner": "SELLER", "msku": "GNT~300506-9-10~RubiesDisneyFroze", "prepOwner": "SELLER", "quantity": 24 }, { "labelOwner": "SELLER", "msku": "GNT~6061626~SpinMasterGamesSkillm", "prepOwner": "SELLER", "quantity": 8 }, { "labelOwner": "SELLER", "msku": "GNT~F2934~MyLittlePonyPlayDoh", "prepOwner": "SELLER", "quantity": 24 }, { "labelOwner": "SELLER", "msku": "GNT~HKV33~PollyPocketMiniToysCampAdve", "prepOwner": "SELLER", "quantity": 32 }, { "labelOwner": "SELLER", "msku": "GNT~TM15537~McFarlaneToysDCMultiverse", "prepOwner": "SELLER", "quantity": 6 }, { "labelOwner": "SELLER", "msku": "SMF~70604~Playmobil70596CityLifeF", "prepOwner": "SELLER", "quantity": 24 } ], "name": "TEST", "sourceAddress": { "addressLine1": "Unit 2 49 Quarry Road", "addressLine2": "Ballydrain", "city": "Lisbane", "companyName": "Ardmillan Trading Limited", "countryCode": "GB", "email": "paul@email.co.uk", "name": "Paul Anderson", "phoneNumber": "07841903333", "postalCode": "BT23 6QQ", "stateOrProvinceCode": "Co. Down" } }, "path": "/inbound/fba/<version>/inboundPlans", "method": "POST" }
返回的错误信息
sp_api.base.exceptions.SellingApiBadRequestException: [{'code': 'InvalidInput', 'message': "9 validation errors detected: Value '[]' at 'request.destinationMarketplaces' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'request.sourceAddress.phoneNumber' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'request.sourceAddress.city' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'request.sourceAddress.countryCode' failed to satisfy constraint: Member must satisfy regular expression pattern: [A-Z]{2}; Value at 'request.sourceAddress.countryCode' failed to satisfy constraint: Member must have length greater than or equal to 2; Value at 'request.sourceAddress.postalCode' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'request.sourceAddress.name' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'request.sourceAddress.addressLine1' failed to satisfy constraint: Member must have length greater than or equal to 1; Value '[]' at 'request.items' failed to satisfy constraint: Member must have length greater than or equal to 1", 'details': ''}]
问题根源分析
错误提示里的关键信号是:API认为destinationMarketplaces是空数组、items是空数组,且sourceAddress的所有字段都不符合长度要求,但你提交的Payload明明有完整数据——这说明API根本没读到你提交的这些字段!
罪魁祸首是你把所有核心字段都包裹在了body这个嵌套层级里,但SP API的createInboundPlan接口要求请求体直接包含destinationMarketplaces、items、name、sourceAddress这些顶级字段,不需要额外的body包裹。当API解析请求时,找不到这些顶级字段,就会默认它们是空值,从而抛出一堆验证错误。
解决步骤
调整请求体结构,移除外层的
body包裹
正确的请求体应该直接把核心字段放在顶层,示例:{ "destinationMarketplaces": [ "A1F83G8C2ARO7P" ], "items": [ // 你的商品列表... ], "name": "TEST", "sourceAddress": { // 你的地址信息... } }如果使用python-amazon-sp-api库,用正确的方式传参
这个库的create_inbound_plan方法不需要你自己组装包含body的字典,直接把参数作为关键字参数传入即可,示例代码:from sp_api.api import InboundFba from sp_api.base import SellingApiAuth, Marketplaces # 初始化客户端 auth = SellingApiAuth( account='your_account', refresh_token='your_refresh_token', client_id='your_client_id', client_secret='your_client_secret', marketplace=Marketplaces.UK ) client = InboundFba(auth=auth) # 调用接口,直接传顶级参数 response = client.create_inbound_plan( destinationMarketplaces=["A1F83G8C2ARO7P"], items=[ { "labelOwner": "SELLER", "msku": "GNT~300506-9-10~RubiesDisneyFroze", "prepOwner": "SELLER", "quantity": 24 }, # 其他商品项... ], name="TEST", sourceAddress={ "addressLine1": "Unit 2 49 Quarry Road", "addressLine2": "Ballydrain", "city": "Lisbane", "companyName": "Ardmillan Trading Limited", "countryCode": "GB", "email": "paul@email.co.uk", "name": "Paul Anderson", "phoneNumber": "07841903333", "postalCode": "BT23 6QQ", "stateOrProvinceCode": "Co. Down" } ) print(response.payload)额外验证:countryCode格式
错误里提到countryCode不符合[A-Z]{2}正则,你的"GB"是完全符合要求的,这个错误是API没读到字段导致的副作用,解决结构问题后会自动消失。
总结
核心问题就是请求体的嵌套层级错误,API找不到你提交的参数,所以返回一堆“空值”相关的验证错误。调整结构后,应该就能顺利通过验证了。
备注:内容来源于stack exchange,提问作者pa1983

