*** Updated:
I’m using the FedEx REST API for image upload (/documents/v1/lhsimages/upload
), and I’m getting back an error:
[errors] => stdClass Object
(
[code] => 1001
[message] => Invalid request: invalid input : Invalid document details
)
I suspect the issue might be related to how I’m passing the data up – typically in a cURL request for the FedEx API you would do
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->apiRequest);
where $this->apiRequest
is a json encoded request body. This works for other API requests. But the Upload Image is a bit tricky because it contains field data plus a file:
document: {"document": {"referenceId": "1234", "name": "LH2.PNG", "contentType": "image/png", "meta": { "imageType": "SIGNATURE","imageIndex": "IMAGE_1"}},"rules": {"workflowName": "LetterheadSignature" }}
attachment: file.PNG
What I was passing was
$body = (object)[
'referenceId' => 'Signature',
'name' => $filename,
'contentType' => $filetype,
'rules' => (object)[
'workflowName' => 'LetterheadSignature',
],
'meta' => (object)[
'imageType' => 'SIGNATURE',
'imageIndex' => 'IMAGE_1',
]
];
$sent_data = [
'document' => json_encode($body),
'attachment' => $file,
];
$this->apiRequest = $sent_data;
I tried building $file
parameter two ways:
$handle = fopen($full_filename, "r");
$file_contents = fread($handle, filesize($full_filename));
fclose($handle);
$file = base64_encode($file_contents);
and also:
$file = curl_file_create($full_filename, $filetype, $filename);
where
$filename = 'image-of-signature.png';
$filetype = 'image/png';
$full_filename = 'path-to-file' . $filename;
Neither worked; both gave me the Invalid request: invalid input : Invalid document details
1001 error.
The FedEx documentation describes the attachment
parameter as:
string <file>
Input the actual document/file to be uploaded.
I have also tried passing the actual path to the file, and of course that doesn’t work either.
2
Answers
@CBroe's comments led to the solution - the attachment should be a CURLFILE. But the real issue is that the FedEx API documentation is wrong. The
document
element and therules
element are at the same level. The correct structure for thebody
isIf you don't realize this, you will get the 1001 error:
or
The original person to point this out was @Emtek in this post: https://stackoverflow.com/a/78697033
The appropriate open-api-json
So maybe add this curl option:
//And build the atttament like this: