Hi, I have a python script that is using the kf API. I wanted to upload a CSV file to the media of a form, to use the data in the pulldata() function. The status code is resulting out to be 200 but when I go to the media section in the browser, I am unable to find any file there.
Please help.
Thank you
Welcome to the community, @pushkar718! Could you share the python script so that the community could also help you out?
url = 'https://kc.kobotoolbox.org/api/v1/forms/<FORM_ID>'
headers = {'Authorization': f'Token {KOBO_TOKEN}'}
xls_path = '<PATH TO FILE>'
with open(xls_path, 'rb') as f:
response = requests.patch(url, headers=headers, files={'xls_file': f})
if response.status_code == 201:
print('Form uploaded successfully.')
else:
print(f'Error uploading form: {response.status_code} {response.content}')
this was the code I have, I have tried it with V1 and V2 both, but nothing worked
import requests
url = ‘https://kc.kobotoolbox.org/api/v1/media’
csv_file_path = ‘asset_list.csv’
auth_token = KOBO_TOKEN
form_id = ‘<FORM_ID>’
media_type = ‘text/csv’
with open(csv_file_path, ‘rb’) as f:
csv_data = f.read()
headers = {
‘Authorization’: f’Token {auth_token}‘,
‘Content-Type’: media_type,
‘Content-Disposition’: f’attachment; filename=“{csv_file_path}”’,
‘X-Content-Type-Options’: ‘nosniff’,
}
data = {
‘name’: csv_file_path,
‘file’: csv_data,
‘type’: media_type,
‘form’: form_id,
}
response = requests.post(url, headers=headers, data=data)
if response.status_code == 201:
print(‘CSV file uploaded successfully!’)
else:
print(f’Error uploading CSV file: {response.text}')
I tried this script also, but error is given
Error uploading CSV file: {“detail”:“Unsupported media type "text/csv" in request.”}