Sending a post to ES

I'm trying to design a custom button on my dashboard that will make a watch for a specific query. Going into the API and editing watches based off of data I have in the dashboard is a bit slow and I'm in the process of trying to just create a simple button that the user will enter the Error they would like to set a watch for and press send.

I'm now just trying to make a POST work for a simple index and I can't get it to work and I'm wondering if there is anything in ES stopping me from doing it or if anyone has any suggestions. Here is what I've written thus far:

 <!DOCTYPE html>
 <html>
 <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.post("172.28.97.54:9200/customer/external?pretty",
        
{
  "name": "Jane Doe"
},
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

Or if anyone has any suggestions on sending posts through the browser url bar, I could make something work with that.

Kibana dev console would help you?

I've been using that, but I'd like to access the kibana dev console with HTML or Javascript in order to send commands from a dashboard. In other words, I want to be able to send commands to ES without necessarily going into the dev API.

Ok. Your previous answer confused me.

Anyway. Did you look at the JS Client we have?

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html

If anyone in the future is interested how I finally did this, here's my code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<script>
var xhr = new XMLHttpRequest();
//Starts the variable xhr as the variable for the request
xhr.open('POST',"http://localhost:9200/customer/external", true);
xhr.setRequestHeader("Content-type", "application/json");
//Runs method 'open' which defines the request
xhr.send ("{\"name\": \"Jane Doe\"}");
//Sends the request

xhr.addEventListener("readystatechange",processRequest, false);
//Listens for the response of the request

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
var jsonObj = JSON.parse(this.responseText);
var jsonPretty = JSON.stringify(jsonObj, null, '\t');
    alert(jsonPretty);
  }
});
</script>
</body>
</html>

This can be used to send a POST request to ES from outside of ES. If you want to have a different request, just change "/customer/external" to whatever you need. Then for the body of the request, if it is complicated, you can download the Postman app where you can change the "body" tab to "raw", copy and paste the exact command like you would from the Kibana API, then press "code">> javascriptXHR. This can be used to create a custom button on your Kibana dash, or whatever other application you can think of that requires you to send POSTs to ES.

Thanks for the support everyone :slight_smile:

2 Likes

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