Hello, I am trying to update some data because the original data has changed and I want to update the same in my form data that was collected about a year ago.
I have already followed the thread on this thread but have not been able to bypass the 400 status code error.
I have even tried to include the relevant data on which the field of concern is dependent on, and still I am getting status code 400.
This is my code:
from config_loader import config
import requests
import json
API endpoint and token
url = “https://kf.kobotoolbox.org/api/v2/assets/{my_asset_uid}/data/bulk”
url_get = “https://kf.kobotoolbox.org/api/v2/assets/{my_asset_uid}/data/” #This is also the url for the API documentation
token = config[‘KOBO’][‘KOBO_API_KEY’]
Headers
headers = {
“Authorization”: f’Token {token}',
“Content-Type”: “application/json”, # Required for JSON payloads
“Accept”: “application/json”
}
Question path and data
question_name = “intro_block/teacher_name”
submission_ids = [‘245014387’, ‘245019690’, ‘245065267’] # Example submission IDs
new_values = [‘T18-KD-Manisha’, ‘T13-KN-Kajal’, ‘T12-KN-Vimla’] # Corresponding new values
Loop through submission IDs and update individually
for submission_id, new_value in zip(submission_ids, new_values):
# fetch existing submission data
response = requests.get(f"{url_get}{submission_id}/", headers=headers)
if response.status_code != 200:
print(f"Failed to fetch submission {submission_id}: {response.text}")
continue
submission_data = response.json()
# Build the payload including required fields to satisfy choice_filter
payload = {
"submission_id": [submission_id],
"data": {
"program_name": submission_data.get("program_name"), # Required for choice_filter
"intro_block/observer_name": submission_data.get("intro_block/observer_name"),
"intro_block/school_name": submission_data.get("intro_block/school_name"), # Required for choice_filter
question_name: new_value, # New value for teacher_name
"intro_block/teaching_type":submission_data.get("intro_block/teaching_type")
}
}
print(f"Updating Submission ID: {submission_id} with Payload: {json.dumps(payload, indent=2)}")
# Send PATCH request
patch_response = requests.patch(url, json=payload, headers=headers)
# Handle response
print(f"Status Code: {patch_response.status_code}")
try:
print(f"Response: {patch_response.json()}")
except requests.exceptions.JSONDecodeError:
print(f"Non-JSON Response: {patch_response.text}")
The error I am getting:
Updating Submission ID: 245014387 with Payload: {
“submission_id”: [
“245014387”
],
“data”: {
“program_name”: “coe_tg”,
“intro_block/observer_name”: “Anjali”,
“intro_block/school_name”: “KD”,
“intro_block/teacher_name”: “T18-KD-Manisha”,
“intro_block/teaching_type”: “independent”
}
}
Status Code: 400
Response: {‘payload’: [‘This field is required.’]}