0

I'm trying to upload evidence file by the documentation of PayPal API (https://developer.paypal.com/docs/api/customer-disputes/v1/)

But I get error 400 - INVALID_REQUEST, MISSING_OR_INVALID_REQUEST_BODY.

I do not understand what I'm doing wrong. This is part of the code that produces form-data:

const str = {
evidences: [
{
evidence_type: "PROOF_OF_FULFILLMENT",
evidence_info: {
tracking_info: [
{
carrier_name: "FEDEX",
tracking_number: "122533485"
}
]
},
notes: "Test"
}
]
}
const data = JSON.stringify(str);

const formData = new FormData();
formData.append('input', data);
formData.append('type', "application/json");
formData.append('file1', fileBuffer, {
filename: "@maor.pdf"
});

And this is the log of the request:

{
method: 'post',
headers: {
'Content-Type': 'multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
Authorization: 'Bearer XXXXXXXXXXX'
},
body: FormData {
_overheadLength: 364,
_valueLength: 75083,
_valuesToMeasure: [],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: false,
_streams: [
'----------------------------736530077015435126960318\r\n' +
'Content-Disposition: form-data; name="input"\r\n' +
'\r\n',
'{"evidences":[{"evidence_type":"PROOF_OF_FULFILLMENT","evidence_info":{"tracking_info":[{"carrier_name":"FEDEX","tracking_number":"122533485"}]},"notes":"Test"}]}',
[Function: bound ],
'----------------------------736530077015435126960318\r\n' +
'Content-Disposition: form-data; name="type"\r\n' +
'\r\n',
'application/json',
[Function: bound ],
'----------------------------736530077015435126960318\r\n' +
'Content-Disposition: form-data; name="file1"; filename="@maor.pdf"\r\n' +
'Content-Type: application/pdf\r\n' +
'\r\n',
<Buffer 25 50 44 46 30 20 6f 62 6a 0a 3c 3c 2f 50 61 67 65 0a 2f 50 61 72 65 6e 74 20 31 20 30 20 52 0a 2f 4d 65 64 69 ... 74855 more bytes>,
[Function: bound ]
],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------736530077015435126960318'
}
}

What am I doing wrong?

Maor agai
  • 221
  • 1
  • 3
  • 11

1 Answers1

1

What am I doing wrong?

You are posting form data. The PayPal REST API does not accept form data, only a JSON string which for this particular call must be part of a multitype content string. See the documentation you link to for what that string should look like; you are not posting such a string, as clearly shown from your logs.

Preston PHX
  • 27,642
  • 4
  • 24
  • 44
  • Ok, thank you. So, you can show me how to convert this cURL to regular post request? – Maor agai May 19 '21 at 07:20
  • It is very simple, you need to build a string with the appropriate format for a "multipart/related" MIME message (specifically), and post that string as your data. Here's an example: https://stackoverflow.com/questions/34905363/create-file-with-google-drive-api-v3-javascript/35182924#35182924 , you can search for others. Make sure they are for "multipart/related", put this in quotes when you search. – Preston PHX May 19 '21 at 09:24