How to send HTTP verbs to ElasticSearch

I am running elasticsearch on my Windows computer on port 9200. I am trying to write a method in C# to create an index, but I keep getting erred out.

Here's a snippet from C#

string jsonPayload = $"{indexName}";
byte[] data = Encoding.ASCII.GetBytes(jsonPayload);
using (var client = new System.Net.WebClient())
{
	client.UploadData(elasticAddress, "PUT", data);
}

Essentially, I'm trying to do what Kibana does from the Dev Tools page: PUT indexname to create an index.

Anyone have advice on how I can use C# to make HTTP calls to my ElasticSearch client running at port 9200? I want to work with HTTP directly, and not go through the Elastic client.

Thanks

This is going to send indexName bytes in the body of the request, which is not correct.

I would recommend starting with one of the official .NET clients for Elasticsearch, either Elasticsearch.Net (low level client) or NEST (high level client). I would suggest the high level client unless you have a good reason for using the low level client.

Then, creating and index is

var elasticAddress= "http://localhost:9200";
var pool = new SingleNodeConnectionPool(new Uri(elasticAddress));
var settings = new ConnectionSettings(pool);

var client = new ElasticClient(settings);

var indexName = "indexName";

var createIndexResponse = client.CreateIndex(indexName);

if (createIndexResponse.IsValid)
{
    Console.WriteLine($"index '{indexName}' created");
}

Check out the client docs for more details.

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