首页 > 解决方案 > 向 PayPal Payouts REST API 发送请求返回 HTTP 代码 204 且无内容

问题描述

我想调用我的服务器,然后它会调用 PayPal Payouts REST API 来进行特定的付款。我设置了必要的帐户,当我使用 Postman 进行尝试时,一切都很好!我调用 URL:https://api.sandbox.paypal.com/v1/payments/payouts与令牌并Content-Type设置为application/json和正文

{
  "sender_batch_header": {
    "sender_batch_id": "Payouts_2018_100013",
    "email_subject": "You have a payout!",
    "email_message": "You have received a payout from ****! Thanks for using our service!"
  },
  "items": [
    {
      "recipient_type": "EMAIL",
      "amount": {
        "value": "9.87",
        "currency": "EUR"
      },
      "receiver": "****@*****.com"
    }]
}

这将返回类似

{
    "batch_header": {
        "payout_batch_id": "F5YLETFEWFLUS",
        "batch_status": "PENDING",
        "sender_batch_header": {
            "sender_batch_id": "Payouts_2018_100012",
            "email_subject": "You have a payout!",
            "email_message": "You have received a payout from realnote! Thanks for using our service!"
        }
    },
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payouts/F5YLETFEWFLUS",
            "rel": "self",
            "method": "GET",
            "encType": "application/json"
        }
    ]
}

使用 HHTP 代码 201(已创建)。payout_batch_id这是完全正确的,当使用来自答案的相应 URL 时,我可以看到我的付款状态。

但是,如果我从我的 NodeJS 服务器尝试相同的调用,就会出现问题。我得到了令牌,一切正常,但随后我创建了这样的请求:

const options = {
    url: "https://api.sandbox.paypal.com/v1/payments/payouts",
    headers: {
      'Content-Type': 'application/json'
    },
    auth: {
      'bearer': token
    },
    form: {
      "sender_batch_header": {
        "sender_batch_id": "***_payment_***",
        "email_subject": "You have a payout!",
        "email_message": "You have received a payout from ****! Thanks for using our service!"
      },
      "items": [
        {
          "recipient_type": "EMAIL",
          "amount": {
            "value": "10.0",
            "currency": "EUR"
          },
          "receiver": "*****@*****.com"
        }]
    }
  };

request然后我使用带有以下代码的模块发送请求:

request.post(options, function(err,httpResponse,body){
    if (err) {
      console.error('Sending money failed:', err);
    } else {
      console.log('Response from PayPal successful!  Server responded with:', body);

      //var payPalAnswer = JSON.parse(body);
    }
  })

但这将导致获得状态码 204(无内容)的答案,难怪它包含无内容,因此不可能像使用 Postman 获得的服务器答案那样获得我的付款状态。我的错误在哪里?

标签: javascriptnode.jspaypal

解决方案


request.post(options, {data: "..."},function(err,httpResponse,body){
    if (err) {
      console.error('Sending money failed:', err);
    } else {
      console.log('Response from PayPal successful!  Server responded with:', body);

      //var payPalAnswer = JSON.parse(body);
    }
  })

您必须使用您的数据作为第二个参数


推荐阅读