PHP API Exception when Batching: Undefined array key "Content-Type"

API version: v24.12.0
I am getting a : Undefined array key "Content-Type" exception when I perform batch operations:

Undefined array key \"Content-Type\" at /workspace/laravel/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php:153)
[stacktrace]
#0 /workspace/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(290): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /workspace/laravel/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php(153): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
#2 /workspace/laravel/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php(280): Aspose\\Words\\ObjectSerializer::parseMultipart()
#3 /workspace/laravel/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(56708): Aspose\\Words\\ObjectSerializer::parseBatchResponse()
#4 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(209): Aspose\\Words\\WordsApi->Aspose\\Words\\{closure}()
#5 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(158): GuzzleHttp\\Promise\\Promise::callHandler()
#6 /workspace/laravel/vendor/guzzlehttp/promises/src/TaskQueue.php(52): GuzzleHttp\\Promise\\Promise::GuzzleHttp\\Promise\\{closure}()
#7 /workspace/laravel/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(167): GuzzleHttp\\Promise\\TaskQueue->run()
#8 /workspace/laravel/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(206): GuzzleHttp\\Handler\\CurlMultiHandler->tick()
#9 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(251): GuzzleHttp\\Handler\\CurlMultiHandler->execute()
#10 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(227): GuzzleHttp\\Promise\\Promise->invokeWaitFn()
#11 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(272): GuzzleHttp\\Promise\\Promise->waitIfPending()
#12 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(229): GuzzleHttp\\Promise\\Promise->invokeWaitList()
#13 /workspace/laravel/vendor/guzzlehttp/promises/src/Promise.php(69): GuzzleHttp\\Promise\\Promise->waitIfPending()
#14 /workspace/laravel/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(56584): GuzzleHttp\\Promise\\Promise->wait()
#15 /workspace/laravel/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(56565): Aspose\\Words\\WordsApi->batchWithHttpInfo()
#16 /workspace/laravel/app/Libraries/AsposeDispatcher.php(172): Aspose\\Words\\WordsApi->batch()

If I build the document without batching it works correctly.

@FFSPUD

The “Undefined array key ‘Content-Type’” exception you are encountering during batch operations in the Aspose PHP API may be related to how the API is handling the request or response headers. This issue can arise if the API does not receive the expected headers, particularly the “Content-Type” header, which is crucial for processing requests correctly.

Here are a few steps you can take to troubleshoot and potentially resolve this issue:

  1. Check API Version: Ensure that you are using the latest version of the Aspose.Words Cloud SDK for PHP. Sometimes, bugs are fixed in newer releases, so updating to the latest version may resolve the issue.

  2. Review Request Structure: Make sure that your batch request is structured correctly. The request should include all necessary headers, including “Content-Type”. If you are manually constructing the request, double-check that you are setting this header appropriately.

  3. Debugging: Add debugging statements to log the headers being sent with your request. This can help you verify whether the “Content-Type” header is being included and if it has the correct value (e.g., application/json).

  4. Sample Code: If you have a working example of a non-batch operation, compare it with your batch operation to identify any discrepancies in how the requests are being formed.

  5. Consult Documentation: Review the official Aspose documentation for any specific requirements or examples related to batch operations. This can provide insights into the expected request format and headers.

If the issue persists after trying these steps, consider reaching out to the Aspose support team or checking the Aspose forums for similar issues reported by other users. They may have additional insights or solutions.

If you need further assistance, feel free to provide more details about your batch request, and I can help you troubleshoot further.

That’s nice AI bot, but it’s not my code. It’s in the API.
Thank you for helping - next time read the post more carfeully ok?

Also, why didn’t you tell me my api version was out of date?
Anyway I’ve updated it and I still get the same problem.

Please share the code of your batch execution and documents along with data(if mail merge exists in your batch)
I will test it to determine why part of the response is not parsed.

Hello,

Here is my code (it is based on the method testBatchWithoudIntermediateResults in /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/tests/Aspose/Words/BatchTests.php:

   global $wordsApi;

    $baseRemoteFolderPath = './vendor/aspose-cloud/aspose-words-cloud';
    $localName = "test_multi_pages.docx";
    $remoteName = "TestGetDocumentParagraphByIndexBatch.docx";
    $subfolder = "DocumentElements/Paragraphs";
    $fullName = $baseRemoteFolderPath . $subfolder . "/" . $remoteName;

    $file = "$baseRemoteFolderPath/TestData/Common/" . $localName;
    $request0 = new UploadFileRequest($file, $fullName);

    $request1 = new BatchPartRequest(new GetParagraphsRequest($name=$remoteName, $nodePath="sections/0", $folder=$baseRemoteFolderPath . $subfolder));

    $request2 = new BatchPartRequest(new GetParagraphRequest($name=$remoteName, $index=0, $nodePath="sections/0", $folder=$baseRemoteFolderPath . $subfolder));

    $request3Body = new \Aspose\Words\Model\ParagraphInsert(array(
        "text" => "This is a new paragraph for your document",
    ));

    $request3 = new BatchPartRequest(new InsertParagraphRequest($name=$remoteName, $paragraph=$request3Body, $nodePath="sections/0", $folder=$baseRemoteFolderPath . $subfolder));

    $request4 = new BatchPartRequest(new DeleteParagraphRequest($name=$remoteName, $index=0, $nodePath="sections/0", $folder=$baseRemoteFolderPath . $subfolder));

    $reportingFolder = "DocumentActions/Reporting";
    $localDocumentFile = "ReportTemplate.docx";
    $localDataFile = file_get_contents("$baseRemoteFolderPath/TestData/" . $reportingFolder . "/ReportData.json");

    $requestReportEngineSettings = new \Aspose\Words\Model\ReportEngineSettings(array(
        "data_source_type" => "Json",
        "data_source_name" => "persons",
    ));
    $buildRequest = new BuildReportOnlineRequest(
        "$baseRemoteFolderPath/TestData/" . $reportingFolder . "/" . $localDocumentFile,
        $localDataFile,
        $requestReportEngineSettings,
        NULL
    );
    $request5 = new BatchPartRequest($buildRequest);
    $upload_result = $wordsApi->uploadFile($request0);

    $result = $wordsApi->batch(array($request1, $request2, $request3, $request4, $request5), false);

The test files are the same ones in the composer package.

The result I get is:

PHP Warning:  Undefined array key "Content-Type" in /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php on line 153
PHP Warning:  Undefined array key 1 in /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php on line 158
PHP Warning:  Undefined array key "RequestId" in /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php on line 268
PHP Warning:  Undefined array key "" in /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php on line 286
PHP Fatal error:  Uncaught Error: Call to a member function getRequest() on null in /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/ObjectSerializer.php:286
Stack trace:
#0 /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(54736): Aspose\Words\ObjectSerializer::parseBatchResponse()
#1 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(209): Aspose\Words\WordsApi->Aspose\Words\{closure}()
#2 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(158): GuzzleHttp\Promise\Promise::callHandler()
#3 /workspace/aspose/vendor/guzzlehttp/promises/src/TaskQueue.php(52): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#4 /workspace/aspose/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(167): GuzzleHttp\Promise\TaskQueue->run()
#5 /workspace/aspose/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(206): GuzzleHttp\Handler\CurlMultiHandler->tick()
#6 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(251): GuzzleHttp\Handler\CurlMultiHandler->execute()
#7 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(227): GuzzleHttp\Promise\Promise->invokeWaitFn()
#8 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(272): GuzzleHttp\Promise\Promise->waitIfPending()
#9 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(229): GuzzleHttp\Promise\Promise->invokeWaitList()
#10 /workspace/aspose/vendor/guzzlehttp/promises/src/Promise.php(69): GuzzleHttp\Promise\Promise->waitIfPending()
#11 /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(54612): GuzzleHttp\Promise\Promise->wait()
#12 /workspace/aspose/vendor/aspose-cloud/aspose-words-cloud/src/Aspose/Words/WordsApi.php(54593): Aspose\Words\WordsApi->batchWithHttpInfo()
#13 /workspace/aspose/test.php(605): Aspose\Words\WordsApi->batch()

Thanks for sharing the code, also, please share what version of PHP you use.

8.1.6, 8.3.12 and 8.3.14

Alright, I will check on all of these versions.

The issue is fixed and will be available in the 25.2 version of SDK. Thanks for your patience.