Data import though Python but server did not accept the data when edit

Hi,

I have been working on a python script to update the data without needing any external tool except python, and I completed it (at least, I am assuming). I can submit to form, and the server accepts the data, but whenever I want to edit it, I get an error. I even added the “version” to my import, but it didn’t work, and I could solve it. I will share the whole code and my information, and if you can tell me what I am missing, I will be happy.

You can find the example import data attached.

Server: Humanitarian
Username: xxxxxxx
form id: aMDhdPuZTsRTFkttv3L79M

data.xlsx (9.9 KB)

image

import pandas as pd
import numpy as np
import requests
import csv
import json
import random

username = input("Username: ")
password = input("password: ")
dataloc = input("Data Location: ")
formid = input("Form ID: ")


df = pd.DataFrame(pd.read_excel(dataloc))
totalrow = len(df.index)
totalimport = len(df.index)
djson = df.to_dict(orient="records")



##Kobo Import
for each in djson:
    metadata = {"meta": {"instanceID":"uuid:d52d6a0d-010a-424e-b201-8a3509ba9e10"}}
    each.update(metadata)
    kobo_importlinl = "https://kc.humanitarianresponse.info/api/v1/submissions.json"
    postrequest = requests.post(kobo_importlinl,json={"id": formid, "submission":each},headers={"Content-Type":"application/json"},auth=(username,password),verify=None)
    if postrequest.status_code ==201:
        print("Successful Import")
        totalrow = totalrow - 1        
        print(F"Total Import: {totalimport} / {totalrow}")
    elif postrequest.status_code == 202:
        print("Duplicated Import")
        totalrow = totalrow - 1
        print(F"Total Import: {totalimport} / {totalrow}")
    else:
        print("Unsuccessful Import")
        totalrow = totalrow - 1
        print(F"Total Import: {totalimport} / {totalrow}")


print("If you have any problem, please contact with me\n osmanislamburcu@gmail.com")

Hi Kal,

I hope you are doing well,

I tried to import data to one of my forms in humanitarian server “aMDhdPuZTsRTFkttv3L79M”. Bu when i want to edit the submission i have face error 400, if it is possible, can you check my form and let me know what is wrong. I couldn’t find it my own.

Regards

@osmanburcu, could you provide more details (like the entire steps) of what you did so we can track it down and see what went wrong?

sure, i will add all details,

1- created a form (“aMDhdPuZTsRTFkttv3L79M”), 4 question
2- I created an import tada with four columns, the same XML header in Kobo, and version of the form.
data.xlsx (9.9 KB)
and XML values of the data.
3- I wrote a python code for the username, password, and location of the import file and formed
4- I got the data as xlsx, and converted it to a dictionary. After that, I added metadata (“instanceID”:“uuid”) information to each element in the directory, ) and converted it to JSON format.
5-then I used to “https://kc.humanitarianresponse.info/api/v1/submissions.json” as the import URL
6-As request,

  1. Added form id
  2. Added the imported data columns in XML value
  3. as a header, i send (Content-Type":"application/JSON()
  4. when I read the status code, I got 201 code as a successful import

After that, I checked the data section in kobo. I saw all my imports, and all values looked in place, and I compared it with a form entry, which looks the same. But when I want to edit the form enketo gives an error “Data server did not accept data. Contact the survey administrator please (400)”.

As far as I can see, the only missing information is the start and end date, but I am unsure if this is the reason. I also added start and end date to forms, It also did not solve the issue

Referencing to this topic: Data import though Python but server did not accept the data when edit

I appreciate your help

For future reference, I found the reason for being unable to edit the form. You need to also provide the form’s uuid code. Without this code, you will not be able to edit the code. You can find an example code below.

In video, i showed how to find the form’s uuid

{
“id”:“{{Form ID}}”,
“submission” :{
“formhub”: {
“uuid”: “{{Form uuid}}”
},
“Kobo Data Column Name 1”: “{{CSV file Column name}}”,
“Kobo Data Column Name 2”: “{{CSV file Column name}}”,
“Kobo Data Column Name 3”: “{{CSV file Column name}}”,
“Kobo Data Column Name 4” : “{{CSV file Column name}}”,
“Kobo Data Column Name 5” : “{{CSV file Column name}}”,
“Kobo Data Column Name 6”: “{{CSV file Column name}}”,
“Kobo Data Column Name 7”: “{{CSV file Column name}}”,
“meta”: {
“instanceID”: “uuid:{{CSV file Column name}}”
}
}
}