API Quiz Creation Issue - "Provided JSON is invalid or does not match the expected format."

Robert.V.8490
Robert.V.8490 Posts: 4 🔍
edited November 11 in Corporate

I am using PHP with Guzzle, and I can't seem to crack through this problem. I have tried a number of config options and how the data is formatted with zero luck. Does anyone have a PHP/Guzzle example when posting data to the Brightspace API? Does anyone know why I am not getting more description error messages back? Any help would be greatly appreciated. Thanks.

Tagged:

Answers

  • Sreelakshmi.N.546
    Sreelakshmi.N.546 Posts: 124 🤝🏼 image
    edited November 4

    Hi Robert,

    Thank you for reaching out to us through the community.

    If you are looking for the JSON format for posting data to the Brightspace API using PHP, can you check if format below helps

    <?php
    require 'vendor/autoload.php';

    use GuzzleHttp\Client;

    $accessToken = 'YOUR_ACCESS_TOKEN';
    $orgUnitId = 12345; // Course ID
    $baseUrl = 'https://yourinstitution.brightspace.com';

    $client = new Client();

    $quizData = [
    "Name" => "Sample API Quiz",
    "IsActive" => true,
    "AutoExportToGrades" => false,
    "AutoSetGraded" => true,
    "AllowHints" => true,
    "DisableRightClick" => false,
    "PreventMovingBackwards" => false,
    "Shuffle" => true,
    "QuestionsPerPage" => 5,
    "PagingType" => 1,
    "DisplayResults" => [
    "DisplayAttemptScore" => true,
    "DisplayAttemptGrade" => true,
    "DisplayAttemptQuestions" => 2,
    "DisplayAttemptAnswers" => true,
    "DisplayAttemptFeedback" => true,
    "DisplayAttemptHint" => true
    ],
    "SubmissionTimeLimit" => [
    "IsEnforced" => false,
    "TimeLimit" => 0,
    "ShowClock" => false
    ],
    "LateSubmissionInfo" => [
    "LateSubmissionOption" => 1
    ],
    "AttemptsAllowed" => 1,
    "AttemptsAllowedType" => 1,
    "RetakeIncorrectOnly" => false,
    "Description" => [
    "Text" => "<p>This quiz was created via the Brightspace API.</p>",
    "Html" => "<p>This quiz was created via the Brightspace API.</p>",
    "IsHidden" => false
    ]
    ];

    try {
    $response = $client->request('POST', "$baseUrl/d2l/api/le/1.71/$orgUnitId/quizzes/", [
    'headers' => [
    'Authorization' => 'Bearer ' . $accessToken,
    'Accept' => 'application/json',
    'Content-Type' => 'application/json'
    ],
    'json' => $quizData
    ]);

    $data = json_decode($response->getBody(), true);
    echo "✅ Quiz created successfully:\n";
    print_r($data);

    } catch (\GuzzleHttp\Exception\RequestException $e) {
    echo "❌ Error creating quiz: " . $e->getMessage() . "\n";
    if ($e->hasResponse()) {
    $error = json_decode($e->getResponse()->getBody(), true);
    print_r($error);
    }
    }

    Thanks,

    Sreelakshmi

  • Robert.V.8490
    Robert.V.8490 Posts: 4 🔍

    Hey Sreelakshmi,

    Thank you for the response. However, I converted the PHP array to a JSON payload for a PostMan request. I keep getting a 400 Bad Request. Are you able to get that payload to work in your environment?

  • Robert.V.8490
    Robert.V.8490 Posts: 4 🔍

    These error messages are pretty frustrating without any details on what is actually wrong… Cool, the JSON payload is wrong… why?? lol

  • Krizzy.S.9720
    Krizzy.S.9720 Posts: 176 image admin
    edited November 10

    Hello @Robert.V.8490 ,

    Thanks for reaching out! We’ve connected your question with our team and are currently awaiting their response.