Issue importing saved objects via API

We are using 7.11.2.

From the documentation (Import objects API | Kibana Guide [7.13] | Elastic) it said "The request body must include the multipart/form-data type."

So we created a python script to "export" and "import" saved objects. When trying to import we've tried the following:

fully_qualified_file_path = r'./local_object_repo/visualization/default_mba_heatmap_visual.json'

req_str = 'https://<our URL>.aws.found.io:9243/s/shared_test/api/saved_objects/_import?overwrite=true'

headers = {'kbn-xsrf': 'true', 'Authorization': 'Basic <encoded key>'}

with open(fully_qualified_file_path, 'r') as f:
kib_response = requests.post(req_str, headers=headers, files=f)

This response is '{"statusCode":400,"error":"Bad Request","message":"[request body.file]: expected value of type [Stream] but got [undefined]"}'

I stepped into the python requests code and can confirm the final headers looked like:
{ 'User-Agent': 'python-requests/2.25.1',
'Accept-Encoding': 'gzip, deflate',
'Accept': '/',
'Connection': 'keep-alive',
'kbn-xsrf': 'true',
'Authorization': 'Basic ',
'Content-Length': '38',
'Content-Type': 'multipart/form-data;
boundary=e59d488e2bdbd28a07eff76e7482b58d'}

I was able to use the same file and:

  • use curl and import the the file.
  • manually import the file using the browser'

In both cases, the visualization was created in the expected space. So I presume the contents of the file are "good".

At this point the only way I can get it to work from python is to "exec" the curl command to the command line . . . which is just . . . not ideal.

Any help would be greatly appreciated.

Thanks,
Kohlman

Not a python expert, but it looks like you are passing the file handle into files directly - but according to documentation is has to be calledfile - this section in the docs of the requests package looks relevant: Quickstart — Requests 2.25.1 documentation

files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

Thanks!! That was the missing link!!!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.