API Quiz Creation Issue - "Provided JSON is invalid or does not match the expected format."
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.
Answers
-
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
-
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?
-
These error messages are pretty frustrating without any details on what is actually wrong… Cool, the JSON payload is wrong… why?? lol
-
Hello @Robert.V.8490 ,
Thanks for reaching out! We’ve connected your question with our team and are currently awaiting their response.
-
It would helpful if, instead of your code, what we saw was the details of the actual request — what does the JSON look like that you’re actually sending to the server?
-
Also, I believe that the call you’re trying to make requires a QuizData input structure (see
https://docs.valence.desire2learn.com/res/quiz.html#Quiz.QuizData) and I’m not sure that the JSON you’re generating will at all match that (for example, your code seems to create a property calledRetakeIncorrectOnlywhereas the documentation shows that property in the QuizData structure should probably be namedIsRetakeIncorrectOnly.QuizData structures are quite large and complex, and Brightspace’s JSON parser can be quite finicky. It’s like best to be able to write your code so that you can first test it to create JSON payloads that you would send to Brightspace, and then check that data against the documentation quite carefully to ensure that you’re sending the JSON exactly as the LMS expects to receive it. (A common place that people often stumble is in confusing the
RichTextandRichTextInputstructure formats: it’s a good idea to always double check that you’re using the right one, in this case, it seems according to the documentation that RichTextInput is what’s expected for rich text used in creating the quiz via this API.)


