Using API to update form data - Getting Status Code 400

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.’]}

Hi @shadsaiddiqui, this might be the solution or not i am not sure. What i see in your code is you are missing an “s”. If you check the documentation, submission_id is plural but you use it single. So if you add an “s” it might solve your problem. Because i am able to do edits though postman without anyproblem.

payload = {
    "submission_ids": [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")
    }
}
1 Like

Hello @osmanburcu , thank you for your response.
I have tried it with “submission_ids” as well and I still get the same error.

I tried it again after your response.

sorry to hear.

is any of these questions are within a group? i believe all these questions are in a group called intro_block, right? if yes, try this one

payload = {
    "submission_ids": [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"),
                            "school_name": submission_data.get("intro_block/school_name"),  # Required for choice_filter
                            "question_name" : new_value,  # New value for teacher_name
                            "teaching_type":submission_data.get("intro_block/teaching_type")
    }
    }
}
1 Like

Hello @osmanburcu
I tried the payload structure that you shared.
I am still getting the same error. Pasting the image below

:confused:

if it is possible, can you share the whole script with me to test individually?

1 Like

Hello, yes I can do that. Although, the whole script is in the post itself.
But I can slo DM it to you.

Hello @osmanburcu just checking in. Any progress on this? I have also been trying a bunch of different things. Nothing has worked so far.