# Trying to add Visualization with API

**URL:** <https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690>\
**Category:** Kibana\
**Created:** [June 27, 2018, 8:30pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690 "2018-06-27T20:30:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobotWarden](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RobotWarden](https://discuss.elastic.co/u/RobotWarden)\
**Post date:** [June 27, 2018, 8:30pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690/1 "2018-06-27T20:30:02Z")

</div>

I've managed to create an index with a curl statement. I was able to to create a visualization, export the json, deleted the visualization, and now I am trying to add it again with a curl statement:

I was referencing this post: [https://github.com/elastic/kibana/pull/11632](https://github.com/elastic/kibana/pull/11632)

My statement:  
`curl -X POST -d '@test.json' http://localhost:5601/api/saved_objects/visualization/test`  
Error:  
{"statusCode":400, "error":"Bad Request", "message":"Request must contain a kbn-xsrf header."}

next attempt:  
`curl -X POST -d '@test.json' -H 'kbn-xsrf:true' http://localhost:5601/api/saved_objects/visualization/test`  
Error:  
{"statusCode":400,"error":"Bad Request","message":child "attributes" fails because ["attributes" is required]. .....}

My json:

> ```
> [
> {
> "_source": {
> "title": "test",
> "visState": "{\"title\":\"Error Timeline\",\"type\":\"timelion\",\"params\":{\"expression\":\".es(q='error', index=logstash-*)\",\"interval\":\"auto\"},\"aggs\":[]}",
> "uiStateJSON": "{}",
> "description": "",
> "version": 1,
> "kibanaSavedObjectMeta": {
> "searchSourceJSON": "{}"
> }
> }
> }
> ]
> 
> ```

---

<div class="post-metadata">

**Author:** ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)\
**Post date:** [June 27, 2018, 9:45pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690/2 "2018-06-27T21:45:25Z")

</div>

I'm not familiar with the API really, but from that pull you linked to, the body of the JSON you have isn't right. In the description of the pull, the following is used:

```auto
curl -X POST -d '{ "attributes": { "title": "Test pattern" } }' http://localhost:5601/api/saved_objects/index-pattern

```

There's a top-level `attributes` property which contains the information about the saved object. That would explain the error you are seeing ("attributes" is required).

Try this in your `test.json`:

```json
{
    "attributes": {
      "title": "test",
      "visState": "{\"title\":\"Error Timeline\",\"type\":\"timelion\",\"params\":{\"expression\":\".es(q='error', index=logstash-*)\",\"interval\":\"auto\"},\"aggs\":[]}",
      "uiStateJSON": "{}",
      "description": "",
      "version": 1,
      "kibanaSavedObjectMeta": {
        "searchSourceJSON": "{}"
      }
    }
  }

```

I'm not sure if that's right, but it seems closer, and may result in another helpful error message...

---

<div class="post-metadata">

**Author:** ![RobotWarden](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RobotWarden](https://discuss.elastic.co/u/RobotWarden)\
**Post date:** [June 28, 2018, 1:49pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690/3 "2018-06-28T13:49:30Z")

</div>

Thank you so much for your response.

I updated the json to reflect what you have there and indeed I got new errors. I also tried the same json that gets exported from kibana after creation and got the same errors.

curl:

`curl -X POST -d '@test.json' -H 'kbn-xsrf:true' http://localhost:5601/api/saved_objects/visualization/test`

error:

```auto
 Warning: Couldn't read data from file "test.json", this makes an empty POST.
{"statusCode":400,"error":"Bad Request","message":"child \"attributes\" fails because {\"attributes\" is required,"validation":{""source":"payload","keys":["attributes"]}}

```

curl:

`curl -X POST -H 'Content-Type: application/json' -d '@test.json' -H 'kbn-xsrf:true' http://localhost:5601/api/saved_objects/visualization/test`

error:

```auto
Warning: Couldn't read data from file "test.json", this makes an empty POST.
{"statusCode":400,"error":"Bad Request","message":"\"value\" must be an object","validation":{"source":"payload","keys":["value"]}}

```

---

<div class="post-metadata">

**Author:** ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)\
**Post date:** [July 2, 2018, 6:49pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690/4 "2018-07-02T18:49:32Z")

</div>

> Warning: Couldn't read data from file "test.json", this makes an empty POST.

Something is wrong with the way you're using curl. That is a curl error... are you sure you have the path right for that file? As it is, your sending a request with an empty body, so any error you see in the response isn't useful.

---

<div class="post-metadata">

**Author:** ![RobotWarden](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@RobotWarden](https://discuss.elastic.co/u/RobotWarden)\
**Post date:** [July 12, 2018, 2:25pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690/5 "2018-07-12T14:25:26Z")

</div>

I was having two separate problems here. My json was not in the format that kibana wanted and my curl statement was not forming the request correctly.

After I created the visualization in kibana and exported it using the UI I was left with a clean JSON. I was trying to use the source{} part of the JSON but, as @Joe_Fleming mentioned, I was missing the attributes. The JSON needed to be edited to fit the following format for my statement:

```
{
	"attributes": {
		"title": "123",
		"visState": "",
		"uiStateJSON": "{}",
		"description": "",
		"version": 1,
		"kibanaSavedObjectMeta": {
			"searchSourceJSON": ""
		}
	}
}

```

The title there is what will show in the saved objects UI in kibana but the ID is in the curl statement:

`curl -XPOST -H 'kbn-xsrf: anything' http://localhost:5601/api/saved_objects/visualization/IDHere -d @location/of/file.json -H 'Content-Type: application/json'`

---

<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:** [August 9, 2018, 2:25pm UTC](https://discuss.elastic.co/t/trying-to-add-visualization-with-api/137690/6 "2018-08-09T14:25:27Z")

</div>

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