Akratos
November 11, 2021, 1:31pm
1
Hi, I have a project with multiples forms uploaded and and I would like to know how can I get the id of certain forms using python and my access token. For now I have this:
import requests
import json
TOKEN = ‘xxxxxxxxxxxxxxxxx’
PARAMS = {‘format’: ‘json’}
HEADERS = {‘Authorization’: f’Token {TOKEN}'}
asset=‘xxxxxxxxxxxxxxxxxxxxx’
URL = ‘https://kobo.humanitarianresponse.info/api/v2/assets/‘+asset+’/data/bulk/ ’
payload = { }
res = requests.patch(url= URL, data= {‘payload’: json.dumps(payload)}, params= PARAMS, headers= HEADERS)
Could you help me with the script? Or if there is a way to do it with Kobo_kpi I would appreciate if you could tell me how to do it.
Josh
November 12, 2021, 5:05pm
3
Hi @Akratos , you can find some helpful examples here:
This is copied from some private correspondence I had a while ago. Perhaps it’s useful to someone in this format, until more official documentation is developed for the new API In writing “new,” I’m contrasting KPI to the old KoBoCAT (/api/v1) API.
KoBo API Examples
I’ve used curl to demonstrate these API calls. You’ll notice that all my curl commands begin with curl --silent --user jnm_api:some-secret-phrase --header 'Accept: application/json' and end by being piped to python -m…
Akratos
November 13, 2021, 3:37am
4
Hi @Josh , I could only get this far. I need to do a search by names (e, “louis”) in the form and get the data of that person:
import requests
import json
TOKEN = ‘xxxxxxxxxxxxxxxxxxxxx’
PARAMS = {‘format’: ‘json’}
HEADERS = {‘Authorization’:f’Token {TOKEN}',‘Accept’: ‘application/json’}
URL = ‘https://kobo.humanitarianresponse.info/api/v2/assets/yyyyyyyyyyyyyyyyyyyyyyy/data/ ’
payload = {‘group_pe65x02/Nombre_de_la_persona_encuestada’: ‘louis’}
res = requests.get(url=URL,headers= HEADERS,params= PARAMS,data= {‘payload’: json.dumps(payload)})
print(res)
The server response is successful but I only get
<Response [200]>
I would like to have the _id of that record or any other data. Could you help me with the script?
Josh
November 15, 2021, 5:50pm
5
Hi @Akratos , you can use the query
parameter to do this kind of lookup through the API. The documentation is here (replace with your asset UID):
https://kobo.humanitarianresponse.info/api/v2/assets/<your asset uid>/data/#query-submitted-data
So your URL should like something like:
https://kobo.humanitarianresponse.info/api/v2/assets/<your asset uid>/data?format=json&query={"group_pe65x02/Nombre_de_la_persona_encuestada": "louis"}
Or in Python, something like:
import requests
import json
TOKEN = 'your_secret_token'
ASSET_UID = 'your_asset_uid'
URL = f'https://kobo.humanitarianresponse.info/api/v2/assets/{ASSET_UID}/data'
PARAMS = {
'format': 'json',
'query': json.dumps({
'group_pe65x02/Nombre_de_la_persona_encuestada': 'louis'
})
}
HEADERS = {
'Authorization': f'Token {TOKEN}'
}
res = requests.get(url=URL, params=PARAMS, headers=HEADERS)
assert res.status_code == 200
print(json.dumps(res.json(), indent=2))
Since you asked about getting <Response [200]>
, it seems you might need to familiarize yourself with the Python requests
library and interacting with data through APIs. This might be a helpful resource: Python & APIs: A Winning Combo for Reading Public Data – Real Python
1 Like
Akratos
November 16, 2021, 12:30am
6
Thank you very much @Josh . Your answer is very helpful and the documentation about python too.
2 Likes