You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While writing a script to create 200.000 artificial submissions with randomized data to evaluate server setup performance, I got stuck for a while by a 500 error while trying to post a form.
It turns out that when submitting a form using Python's requests API, the data= (or json=) parameters NEED to be a Python dictionary.
I got fooled by the API docs in the page to think the submission parameter had to be a STRING containing the JSON, and the server response instead of a 400 was a very unhelpful 500. In addition to that, without setting up Sentry I was unable to get a backtrace of any kind from the logs that would have helped a lot as you can see here:
Backtrace
onadata/apps/api/viewsets/xform_submission_api.py in create_instance_from_json at line 70
AttributeError: 'str' object has no attribute 'get'
Proposed fixes
I'm not sure what's the best "fix" for this: its true it's a data submission problem in the user data, on the other hand error reporting, logging and examples could be improved quite easily.
On one side it might be possible to change the code in create_instance_from_json to check if both the whole of request.data is a string (to fix the 500) and after that a check if submission is a string, then throw a 400 telling users they need to send objects/dictionaries and not strings (the 400 if you send submission as string is a not very helpful "invalid JSON at line 0 position 0: '")
On the other side it might be enough to add a Python example to the API page at https://kc.yourkoboserver.org/api/v1/submissions like:
The problem
While writing a script to create 200.000 artificial submissions with randomized data to evaluate server setup performance, I got stuck for a while by a 500 error while trying to post a form.
It turns out that when submitting a form using Python's requests API, the
data=
(orjson=
) parameters NEED to be a Python dictionary.I got fooled by the API docs in the page to think the
submission
parameter had to be a STRING containing the JSON, and the server response instead of a 400 was a very unhelpful 500. In addition to that, without setting up Sentry I was unable to get a backtrace of any kind from the logs that would have helped a lot as you can see here:Backtrace
onadata/apps/api/viewsets/xform_submission_api.py in create_instance_from_json at line 70
AttributeError: 'str' object has no attribute 'get'
Proposed fixes
I'm not sure what's the best "fix" for this: its true it's a data submission problem in the user data, on the other hand error reporting, logging and examples could be improved quite easily.
On one side it might be possible to change the code in
create_instance_from_json
to check if both the whole ofrequest.data
is a string (to fix the 500) and after that a check ifsubmission
is a string, then throw a 400 telling users they need to send objects/dictionaries and not strings (the 400 if you send submission as string is a not very helpful "invalid JSON at line 0 position 0: '")On the other side it might be enough to add a Python example to the API page at
https://kc.yourkoboserver.org/api/v1/submissions
like:The text was updated successfully, but these errors were encountered: