Paypal Customer Dispute API - 提供证据总是返回 MISSING_OR_INVALID_REQUEST_BODY
Posted
技术标签:
【中文标题】Paypal Customer Dispute API - 提供证据总是返回 MISSING_OR_INVALID_REQUEST_BODY【英文标题】:Paypal Customer Dispute API - Provide Evidence always returns MISSING_OR_INVALID_REQUEST_BODY 【发布时间】:2021-05-01 05:18:30 【问题描述】:在验证争议案例的HATEOAS links 上有/provide-evidence
后,在索赔阶段向Paypal Disputes 提供证据时,我一直在尝试上传pdf 文件,下面的curl 代码在CLI 中有效:
$ curl -v -X POST https://api.sandbox.paypal.com/v1/customer/disputes/PP-D-12345/provide-evidence \
-H "Content-Type: multipart/related" \
-H "Authorization: Bearer AccessToken" \
-F 'input="evidences":["evidence_type": "PROOF_OF_FULFILLMENT", "evidence_info": "tracking_info": ["carrier_name": "FEDEX", "tracking_number": "123456789"], "notes": "Test"];type=application/json' \
-F 'file1=@D:\Sample\storage\app\paypal\sample.pdf'
但是,当转换为 php 时,无论是使用 curl
还是 guzzle
,API 都会返回 VALIDATION_ERROR - MISSING_OR_INVALID_REQUEST_BODY
,我尝试了几乎所有可能的方法,但错误是一致的。
使用 Guzzle:(尽量复制上面的工作 curl,由于 Guzzle 无法发送multipart/related
请求,我只好modify the content-type manually)。
$pdf = 'D:\Sample\storage\app\paypal\sample.pdf';
$input = [
'evidences' => [
[
'evidence_type' => 'PROOF_OF_FULFILLMENT',
'evidence_info' => [
'tracking_info' => [
'carrier_name' => "FEDEX",
'tracking_number' => '122533485'
]
],
'notes' => 'Test',
],
]
];
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://api.sandbox.paypal.com',
'timeout' => 2.0,
'version' => 1.1
]);
$options = [
'headers' => [
'Authorization' => "Bearer $token",
],
'multipart' => [
[
'name' => 'input',
'contents' => json_encode($input),
'headers' => ['Content-Type' => 'application/json']
],
[
'name' => 'file1',
'contents' => fopen($pdf, 'r'),
'filename' => 'sample.pdf',
'headers' => ['Content-Type' => 'application/pdf']
],
]
];
$url = '/v1/customer/disputes/'.$disputeId.'/provide-evidence';
$headers = isset($options['headers']) ? $options['headers'] : [];
$body = new \GuzzleHttp\Psr7\MultipartStream($options['multipart']);
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, $body, '1.1');
$modify['set_headers']['Content-Type'] = 'multipart/related; boundary=' . $request->getBody()->getBoundary();
$request = \GuzzleHttp\Psr7\modify_request($request, $modify);
$response = $client->send($request);
当我执行正常的 multipart/form-data 请求时,上面的 guzzle 代码仍然返回 VALIDATION_ERROR - MISSING_OR_INVALID_REQUEST_BODY
和相同的结果。
可能是什么问题?任何想法或建议都会非常有帮助,谢谢。
【问题讨论】:
【参考方案1】:$input = [
'evidences' => [
[
'evidence_type' => 'PROOF_OF_FULFILLMENT',
'evidence_info' => [
'tracking_info' => [
'carrier_name' => "FEDEX",
'tracking_number' => '122533485'
]
],
'notes' => 'Test',
],
]
];
这是你的$input,如果你json_encode()
它,你会得到
"evidences":
[
"evidence_type":"PROOF_OF_FULFILLMENT",
"evidence_info":
"tracking_info":
"carrier_name":"FEDEX",
"tracking_number":"122533485"
,
"notes":"Test"
]
哪个不与您的请求正文匹配,
我通过插入另一个方括号更改了您的 $input,
$input = [
'evidences' => [
[
'evidence_type' => 'PROOF_OF_FULFILLMENT',
'evidence_info' => [
'tracking_info' => [[
'carrier_name' => "FEDEX",
'tracking_number' => '122533485'
]]
],
'notes' => 'Test',
],
]
];
现在它符合你的 curl 请求,
"evidences":
[
"evidence_type":"PROOF_OF_FULFILLMENT",
"evidence_info":
"tracking_info":
[
"carrier_name":"FEDEX",
"tracking_number":"122533485"
]
,
"notes":"Test"
]
我希望它有所帮助。如果它不起作用,那么问题可能出在其他地方但请求正文会匹配,您可以先尝试 guzzle 调用。
【讨论】:
谢谢,我的错,我太着迷于文件上传,错过了我的部分 json 数据?以上是关于Paypal Customer Dispute API - 提供证据总是返回 MISSING_OR_INVALID_REQUEST_BODY的主要内容,如果未能解决你的问题,请参考以下文章
hihoCoder #1582 : Territorial Dispute 凸包
hihoCoder #1582 Territorial Dispute 几何凸包
hihocoder 1582 : Territorial Dispute (计算几何)(2017 北京网络赛E)
hihoCoder 1582 Territorial Dispute 凸包(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)