Vignesh
Vignesh

Reputation: 35

I want to send a http request (Post, Get etc) to elasticsearch with authorization in C#. How do I do that?

I want to send and receive request to elastic client which is hosted in my local machine. I'm able to get it running and send request via cURL in command prompt. For eg:

By running this in my cmd, I'm able to view the health of my cluster.

With this command, I'm pushing "products-bulk.json", a file in the same directory to the elastic server.

I have to use --insecure as else it gives some certificate error and I think I may have to use that as well if I try in C#. (An alternate I found was --cacert but that has some common issues with elastic search it seems.).

How could I access the server and rewrite these commands in C#. I tried looking through HTTP Client documentation and few queries but found it hard to understand and get it work. So Any help is greatly appreciated.

I'm aware that this could be done easily in Kibana but I'm looking for this to be done in C#.

PS: I'm new to programming. Please forgive if the terminologies used are inaccurate and the doubt is insignificant.

Upvotes: 0

Views: 1555

Answers (1)

LeBigCat
LeBigCat

Reputation: 1770

  1. install Nest package from nugget (main version matchting with your es version)

  2. You can use LowLevel client to sent any json command.

For authentification refer to: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/configuration-options.html

Exemple:

Elasticsearch.Net.ElasticLowLevelClient lowClient = new Elasticsearch.Net.ElasticLowLevelClient(yourAuth);
            var te = lowClient.Bulk<BulkResponse>(indexTargetet, yourjson);

Upvotes: 1

Related Questions