Problems encountered when submitting answers to the server
The location of the problem is ( func submit_quiz_answers() : )
This is my code:
extends Control
var base_url = “https://lms.jinhyung.kim/api/quiz/”
var chapter_id = 3
var submit_url = base_url + str(chapter_id) + “/submit”
@onready var http_request = $HTTPRequest
@onready var questions_container = $QuestionsContainer
@onready var submit_button = $SubmitButton
var quiz_questions =
func _ready():
if submit_button.is_connected(“pressed”, Callable(self, “_on_submit_button_pressed”)):
submit_button.disconnect(“pressed”, Callable(self, “_on_submit_button_pressed”))
submit_button.connect("pressed", Callable(self, "_on_submit_button_pressed"))
var token = Globals.jwt_token
print("JWT Token: ", token)
get_quiz_questions(token)
$LogOutImage.visible = false
func get_quiz_questions(token):
var url = base_url + str(chapter_id)
var headers = ["Authorization: Bearer " + token]
print("Requesting URL: ", url)
print("Request Headers: ", headers)
http_request.request(url, headers, HTTPClient.METHOD_GET)
http_request.connect("request_completed", Callable(self, "_on_get_quiz_request_completed"))
func _on_get_quiz_request_completed(result, response_code, headers, body):
print("Response Code: ", response_code)
if response_code == 200:
var json_result = JSON.new()
var parse_error = json_result.parse(body.get_string_from_utf8())
if parse_error == OK:
quiz_questions = json_result.data
print("Quiz questions: ", quiz_questions)
display_all_questions()
else:
print("JSON parse error")
else:
print("Error fetching quiz questions: ", response_code)
print("Response body: ", body.get_string_from_utf8())
func display_all_questions():
clear_container(questions_container)
questions_container.columns = 2
if quiz_questions.size() > 0:
for i in range(quiz_questions.size()):
var question = quiz_questions[i]
var vbox = VBoxContainer.new()
vbox.name = "QuestionContainer_" + str(i + 1)
questions_container.add_child(vbox)
var question_label = Label.new()
question_label.text = str(i + 1) + ". " + question.content
vbox.add_child(question_label)
for j in range(question.options.size()):
var option = question.options[j].content
var option_letter = String(char(97 + j))
var option_checkbox = CheckBox.new()
option_checkbox.text = " " + option_letter + ") " + option
option_checkbox.set_meta("question_id", question.id)
option_checkbox.set_meta("option_id", question.options[j].id)
vbox.add_child(option_checkbox)
func clear_container(container):
for child in container.get_children():
container.remove_child(child)
child.queue_free()
func _on_submit_button_pressed():
submit_quiz_answers()
func submit_quiz_answers():
var answers = {
“answers”:
}
for i in range(quiz_questions.size()):
var question_id = quiz_questions[i].id
var selected_option_id = get_selected_option_id(i)
if selected_option_id != null:
answers["answers"].append({ "quizId": question_id, "optionId": selected_option_id })
var json_data = JSON.stringify(answers)
var token = Globals.jwt_token
var headers = [
"Authorization: Bearer " + token,
"Content-Type: application/json"
]
print("Submitting answers to: ", submit_url)
print("Headers: ", headers)
print("Request Body: ", json_data)
http_request.request(submit_url, headers, HTTPClient.METHOD_POST, json_data)
http_request.connect("request_completed", Callable(self, "_on_submit_quiz_request_completed"))
func get_selected_option_id(question_index):
var vbox = questions_container.get_child(question_index)
for child in vbox.get_children():
if child is CheckBox and child.is_pressed():
return child.get_meta(“option_id”)
return null
func _on_submit_quiz_request_completed(result, response_code, headers, body):
print("Response Code (Submit): ", response_code)
if response_code == 200:
var json_result = JSON.new()
var parse_error = json_result.parse(body.get_string_from_utf8())
if parse_error == OK:
var submission_result = json_result.data
print("Submission result: ", submission_result)
var result_text = "\n\nQuiz submission results:\n"
result_text += "Correct Answers: " + str(submission_result.correctAnswers) + "\n"
result_text += "Total Score: " + str(submission_result.totalScore)
var result_label = Label.new()
result_label.text = result_text
questions_container.add_child(result_label)
else:
print("JSON parse error")
else:
print("Error submitting answers: ", response_code)
print("Response body: ", body.get_string_from_utf8())
func _on_board_button_pressed() → void:
get_tree().change_scene_to_file(“res://Scene/main_board.tscn”)
This is issue:
Submitting answers to: https://lms.jinhyung.kim/api/quiz/3/submit
Headers: [“Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjk3LCJyb2xlIjoidXNlciIsImlhdCI6MTcyNzI1MzgyN30.gHzlSg9EDntiq1gN5OmaemtqpICk2X8cNxNUHJJuYdo”, “Content-Type: application/json”]
Request Body: {“answers”:[{“optionId”:6,“quizId”:2},{“optionId”:1,“quizId”:1},{“optionId”:30,“quizId”:8},{“optionId”:27,“quizId”:7}]}
Response Code: 500
Error fetching quiz questions: 500
Response body: {“statusCode”:500,“message”:“Internal server error”}
Response Code (Submit): 500
Error submitting answers: 500
Response body: {“statusCode”:500,“message”:“Internal server error”}