Coinbase Commerce API报400错误 提示缺失pricing_type参数排查
问题产生原因
构造POST请求体时错误地将所有业务参数嵌套在了query对象内部,Coinbase Commerce创建charge接口要求所有必填参数直接放在请求体的最外层,不接受额外的嵌套包装层,因此接口无法读取到顶层的pricing_type字段,触发参数缺失报错。
错误结构验证
当前代码序列化后实际发送的请求体结构如下:
{ "query": { "name": "The Sovereign Individual", "description": "Mastering the Transition to the Information Age", "local_price": { "amount": "100.00", "currency": "USD" }, "pricing_type": "fixed_price", "metadata": { "customer_id": "id_1005", "customer_name": "Satoshi Nakamoto" }, "redirect_url": "https://charge/completed/page", "cancel_url": "https://charge/canceled/page" } }
接口校验时只会遍历请求体的顶层字段,该结构下顶层只有query字段,没有pricing_type,因此返回Required parameter missing: pricing_type错误,和你代码里有没有在内部对象写这个字段没有关系。
解决方法
移除请求体序列化时多余的query嵌套层,直接将构造好的charge参数对象序列化为请求体即可,修正后的代码如下:
const chargeParams = { name: 'The Sovereign Individual', description: 'Mastering the Transition to the Information Age', local_price: { amount: '100.00', currency: 'USD', }, pricing_type: 'fixed_price', metadata: { customer_id: 'id_1005', customer_name: 'Satoshi Nakamoto', }, redirect_url: 'https://charge/completed/page', cancel_url: 'https://charge/canceled/page', }; const opts = { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CC-Api-Key': 'api_key', 'X-CC-Version': '2018-03-22', }, body: JSON.stringify(chargeParams), }; fetch('https://api.commerce.coinbase.com/charges', opts) .then(res => res.json()) .then(jsonRes => console.log(jsonRes));
补充说明
- 修复后请求体顶层会直接包含
pricing_type等所有接口要求的参数,可直接通过参数校验 - 测试调用时请将
X-CC-Api-Key字段替换为你自己的有效API密钥,避免出现鉴权失败问题
内容的提问来源于stack exchange,提问作者Ifeoluwa Alao
相关产品推荐
相关产品推荐

