skip to Main Content

*** 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


  1. Chosen as BEST ANSWER

    @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 the rules element are at the same level. The correct structure for the body is

          $body = (object)[
             'document' => (object)[
                'referenceId' => 'Letterhead',
                'name' => $filename,
                'contentType' => $filetype,
                'meta' => (object)[
                   'imageType' => 'LETTERHEAD',
                   'imageIndex' => 'IMAGE_2',
                ],
             ],
             'rules' => (object)[
                'workflowName' => 'LetterheadSignature',
             ]
          ];
    

    If you don't realize this, you will get the 1001 error:

            "message": "Invalid request: invalid input : Invalid document details"
    

    or

            "message": "Invalid request: invalid input : WorkFlow Name"
    

    The original person to point this out was @Emtek in this post: https://stackoverflow.com/a/78697033


  2. The appropriate open-api-json

    {
      "requestBody": {
        "content": {
          "multipart/form-data": {
            "schema": {"$ref": "#/components/schemas/FullSchema-ImageUploadServiceInputVO"},
            "examples": {
              "Full-Schema": {"value": "document: {"document": {"referenceId": "1234", "name": "LH2.PNG",    "contentType": "image/png",    "meta": { "imageType": "SIGNATURE","imageIndex": "IMAGE_1"}},"rules": {"workflowName": "LetterheadSignature" }}nattachment: file.PNG"}
            }
          }
        }
      }
    }
    

    So maybe add this curl option:

    CURLOPT_HTTPHEADER => array(
        'Content-Type: multipart/form-data'
      )
    

    //And build the atttament like this:

    $sent_data = [
                'document' => json_encode($body),
                'attachment' => new CURLFILE($full_filename)
             ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search