# Issue importing saved objects via API

**URL:** <https://discuss.elastic.co/t/issue-importing-saved-objects-via-api/276287>\
**Category:** Kibana\
**Created:** [June 17, 2021, 3:35pm UTC](https://discuss.elastic.co/t/issue-importing-saved-objects-via-api/276287 "2021-06-17T15:35:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)\
**Post date:** [June 17, 2021, 3:35pm UTC](https://discuss.elastic.co/t/issue-importing-saved-objects-via-api/276287/1 "2021-06-17T15:35:36Z")

</div>

We are using 7.11.2.

From the documentation ([Import objects API | Kibana Guide [7.13] | Elastic](https://www.elastic.co/guide/en/kibana/current/saved-objects-api-import.html)) 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

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [June 21, 2021, 7:45am UTC](https://discuss.elastic.co/t/issue-importing-saved-objects-via-api/276287/2 "2021-06-21T07:45:12Z")

</div>

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 called`file` - this section in the docs of the requests package looks relevant: [Quickstart — Requests 2.25.1 documentation](https://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file)

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

```

---

<div class="post-metadata">

**Author:** ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)\
**Post date:** [June 21, 2021, 6:25pm UTC](https://discuss.elastic.co/t/issue-importing-saved-objects-via-api/276287/3 "2021-06-21T18:25:05Z")

</div>

Thanks!! That was the missing link!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 19, 2021, 6:25pm UTC](https://discuss.elastic.co/t/issue-importing-saved-objects-via-api/276287/4 "2021-07-19T18:25:48Z")

</div>

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