I have the following curl command:
curl -X POST "_my_username_:[email protected]: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?
curl -vwill provde you with all the headers of the HTTP request. Consider reviewing the debug information of the request module (somehow) and comparing it to the working curl request. You can then concentrate your efforts on the missing (or different) HTTP headers. If you don't know how to get debug info fromrequests(I don't yet) you may consider doing a packet capture to review the HTTP headers.