javascript - 向 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 获得的服务器答案那样获得我的付款状态。我的错误在哪里?
解决方案
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);
}
})
您必须使用您的数据作为第二个参数
推荐阅读
- javascript - 追踪涉及 Meteor 和多个 DDP 连接的内存泄漏
- python - 是否可以从 __init__.py 扩展一个类
- elasticsearch - 通过 kubernetes-ingress 流利到 elasticsearch
- flutter - 使用 Flutter 在 RxDart 中等待 observable 完成
- python - lmfit - 最小化器不接受 scipy 最小化器关键字参数
- java - 在Processing中从X点到Y点绘制圆柱体
- ruby - Sinatra 和 post datamapper 关联
- javascript - 无法保存第一次单击的状态
- haskell - 接受 3 个字符串的函数,其中第一个是 3rd 的一部分,并用第二个字符串更改 3rd 中的第一个字符串
- powershell - 需要在 CSV 中为多个主机运行脚本