Trying to add Visualization with API


(robot) #1

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

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": "{}"
      }
    }
  }
]

(Joe Fleming) #2

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:

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:

{
    "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...


(robot) #3

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:

 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:

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"]}}

(Joe Fleming) #4

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.


(robot) #5

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'


(system) #6

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