json_encode error: Type is not supported on multipart/form-data

14 viewsfedexjsonphp
0

I’m configuring FedEx trade documents upload API. I tried to follow the documentation but I got error

json_encode error: Type is not supported

This is my payload example.

try{
      $document = [
        "workflowName"=> "ETDPostshipment",
        "carrierCode"=> "FDXE",
        "name"=> 'invoiceFileName',
        "contentType"=> 'application/pdf',
        "meta"=> [
          "shipDocumentType"=> "COMMERCIAL_INVOICE",
          "trackingNumber"=> "794988380825",
          "shipmentDate"=> "2024-02-26",
          "originCountryCode"=> "JP",
          "destinationCountryCode"=>  $country_code
        ]
      ];
      
      $body = [
        [
            'name' => 'document',
            'contents' => json_encode($document)
        ],
        [
            'name' => 'attachment',
            'contents' => fopen(public_path('storage/invoice/invoiceFileName'), 'r'),
            'filename' => $shipping['invoiceFileName']

        ]
      ];
      $response = $this->uploader->post($path, ['multipart' => $body]);
      $data = json_decode($response->getBody(), true);
      Log::info('Document uploaded successfully.', ['response' => $data]);
      return $data;
    }
    catch(RequestException $e){
      Log::error('Request Exception: ' . $e->getMessage(), ['code' => $e->getCode()]);
    }
    catch (Throwable $e){
      Log::error('Unexpected Exception: ' . $e->getMessage());
      return;
    }
  }

What is wrong with this code?