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

解决PayPal Payouts请求中"Unexpected end of JSON input"错误

解决PayPal Payouts API调用的JSON解析错误

问题分析

你遇到的FetchError: invalid json response body本质是因为PayPal返回了500 Internal Server Error,且响应体为空(size: 0),此时调用.json()自然会失败——因为没有JSON内容可解析。

解决方案步骤

1. 优先排查请求合法性

500错误虽标注为服务器内部问题,但多数情况是请求不符合API规范触发的:

  • 确认HTTP方法为POST,Payouts API创建批量付款必须使用POST请求
  • 检查请求头:必须包含Content-Type: application/json,以及有效的Authorization: Bearer <你的访问令牌>
  • 验证请求体参数:比如收款方的recipient_type必须是EMAIL/PHONE/PAYPAL_ID之一;amount的currency需符合ISO 4217标准(如USD);金额数值格式合法
  • 确认沙箱账号已开启Payouts权限,且账号有足够的测试余额

2. 优化代码错误处理逻辑

不要直接调用.json(),先判断响应状态,避免空解析报错:

async function callPayPalPayouts() {
  try {
    const response = await fetch('https://api-m.sandbox.paypal.com/v1/payments/payouts', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
      },
      body: JSON.stringify(/* 你的请求体内容 */)
    });

    if (!response.ok) {
      // 非2xx状态码时,先读取文本获取可能的错误信息
      const errorText = await response.text();
      console.error(`PayPal请求失败,状态码: ${response.status},错误信息: ${errorText || '无详细信息'}`);
      throw new Error(`请求失败,状态码${response.status}`);
    }

    const data = await response.json();
    // 处理正常响应数据
  } catch (err) {
    console.error('调用失败:', err);
  }
}

3. 用工具辅助排查

  • 用Postman或curl直接发送相同请求,排除代码层面的问题:
    curl -X POST https://api-m.sandbox.paypal.com/v1/payments/payouts \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
      -d '{
        "sender_batch_header": {
          "sender_batch_id": "batch_123",
          "email_subject": "Test Payout"
        },
        "items": [
          {
            "recipient_type": "EMAIL",
            "amount": {
              "value": "10.00",
              "currency": "USD"
            },
            "receiver": "test@example.com"
          }
        ]
      }'
    
  • 登录PayPal开发者后台,查看API请求日志,里面会有更详细的错误触发原因(比如参数校验失败、权限不足等)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:07:27