You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JavaScript Fetch调用VerifyMe API失败,报400错误求助

问题描述

尝试用JavaScript的fetch调用VerifyMe地址验证API,代码如下:

const addressReq = await fetch(`https://vapi.verifyme.ng/v1/verifications/addresses`, {
                    headers:{
                        'Authorization':`Bearer ${AllKeys.verifyNINKey}`,
                        'Content-Type': 'application/json'
                    },
                    method:'POST',
                    body:{
                        'reference':`${id}`,
                        'city':`${city}`,
                        'state':`${state}`,
                        'landmark':`${city} ${estate && estate + ' estate'}`,
                        'lga': `${lga}`,
                        'street':`${street_number} ${street_name} ${estate} ${city}`,
                        "applicant.firstname" : `${displayName}`,
                        "applicant.lastname" : `${lastName}`,
                        "applicant.dob" : `${rows.birth_info}`,
                        "applicant.phone" : `${number}`,
                        "applicant.idType" : 'KYC',
                        "applicant.idNumber" : `${number}`,
                        // "applicant.firstname" : `${displayName}`,
                        // "applicant": {
                        //     'firstname':`${displayName}`,
                        //     'lastname':`${lastName}`,
                        //     'dob':`${rows.birth_info}`,
                        //     'phone':`${number}`,
                        //     'idType':'KYC',
                        //     'idNumber':`${number}`,
                        // }
                    }
                }).then(async(res) => {
                    console.log(await res.json())
                    dispatch({ type:'UPDATE_STATUS',payload: {status:'guarantor'} })
                }).catch(err => console.log(err))

调用后持续收到400错误,错误信息:

Object {
  "error": "Bad Request",
  "message": Array [
    "applicant should not be null or undefined",
    "state should not be empty",
    "state must be a string",
    "street should not be empty",
    "street must be a string",
    "lga should not be empty",
    "lga must be a string",
  ],
  "statusCode": 400,
}

用Postman调用正常,但Pipedream的webhook收不到请求,说明调用确实失败。

修复方案

1. 必须将请求体转为JSON字符串

fetch不会自动将对象类型的body转为JSON格式,服务器无法解析原始对象,这是核心问题。Postman会自动处理JSON序列化,所以能正常工作。修复时用JSON.stringify()包裹body对象。

2. 修正Applicant的格式

API要求applicant是嵌套对象,而非applicant.firstname这种扁平键名,启用你注释掉的嵌套写法,删除所有带applicant.前缀的键。

3. 确保变量非空

错误提示多个字段不能为空,要保证state、lga、street_number/street_name等变量有有效值,避免拼接后为空字符串。可以给变量加默认值(比如state || ""),或者提前做非空校验。

修复后的代码示例

const addressReq = await fetch(`https://vapi.verifyme.ng/v1/verifications/addresses`, {
  headers: {
    'Authorization': `Bearer ${AllKeys.verifyNINKey}`,
    'Content-Type': 'application/json'
  },
  method: 'POST',
  // 用JSON.stringify序列化请求体
  body: JSON.stringify({
    'reference': `${id}`,
    'city': `${city || ""}`,
    'state': `${state || ""}`,
    'landmark': `${city} ${estate ? estate + ' estate' : ""}`,
    'lga': `${lga || ""}`,
    'street': `${street_number || ""} ${street_name || ""} ${estate || ""} ${city || ""}`.trim(),
    // 使用嵌套的applicant对象
    "applicant": {
      'firstname': `${displayName}`,
      'lastname': `${lastName}`,
      'dob': `${rows.birth_info}`,
      'phone': `${number}`,
      'idType': 'KYC',
      'idNumber': `${number}`,
    }
  })
}).then(async(res) => {
  console.log(await res.json())
  dispatch({ type:'UPDATE_STATUS',payload: {status:'guarantor'} })
}).catch(err => console.log(err))

内容的提问来源于stack exchange,提问作者Alexander Obidiegwu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 15:25:22