I have the following curl command:
curl -X POST "_my_username_:_my_password_@10.2.25.209:5601/api/saved_objects/_import" -H "kbn-xsrf: true" --form file=@V:kibana\IndexPatterns\events.ndjson
Which works perfectly (import index pattern into elasticsearch), but I'm trying to convert it to Python requests. I tried several ways, including the following:
files = {'file': '@' + args.kibana_index_pattern_path}
res = requests.post("http://{0}:{1}@{2}:5601/api/saved_objects/_import".format(args.elastic_username, args.elastic_password, args.kibana_host),
headers={'kbn-xsrf': 'true'}, data=files)
files = {'file': '@' + args.kibana_index_pattern_path}
res = requests.post("http://{0}:{1}@{2}:5601/api/saved_objects/_import".format(args.elastic_username, args.elastic_password, args.kibana_host),
headers={'kbn-xsrf': 'true', 'Content-Type': 'text/plain'}, files=files)
With different combinations of with or without the @
, files as a single string instead of dictionary, ect. I keep getting errors on bad requests and invalid content types (for example: {'message': 'Unsupported Media Type', 'error': 'Unsupported Media Type', 'statusCode': 415}).
Note that there are some tools to convert curl to requests, but all the ones I tried don't recognize the files param, either ignoring it or throwing exception. The command itself, however, works.
What am I doing wrong here?
Try the following:
import requests
from requests.auth import HTTPBasicAuth
username = '_my_username_'
password = '_my_password_'
headers = {'kbn-xsrf': 'true'}
upload_url = "http://10.2.25.209:5601/api/saved_objects/_import"
files = {'file': open('V:\algotec\analytics\install\Kibana\IndexPatterns\events.ndjson', 'rb')}
r = requests.post(upload_url, headers=headers, auth=HTTPBasicAuth(username, password), files=files)
print(r.status_code)
If you receive a bad request with this error
error: 'Bad Request', message: 'Request must contain a kbn-xsrf header.'
Modify the header information as per the below and re-try.
headers = {
'Content-Type': 'application/x-ndjson',
'kbn-xsrf': 'anything',
'Accept': 'application/x-ndjson'
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.