Perform CRUD Operation on Elasticsearch With REST API

Hi Team,

Could anyone share how to perform CRUD operation in Elastic search with REST API. I had tried the below one with the curl command but getting error as "curl: (52) Empty reply from server" . Could you please guide me.

[root@prestovm1 ~]# curl -XPUT http://localhost:9200/my_index/my_type -curl -H 'Content-Type: application/json' -d '{"user": "Phil","message":"Hello World!"}'*
curl: (52) Empty reply from server*

Thanks,
Debasis

If you are using a recent version of Elasticsearch, like 8.x, most likely you need to run something like:

curl -XPUT https://localhost:9200/my_index/my_type \
    --user elastic \
    -k \
    -H 'Content-Type: application/json' \
    -d '
{ 
  "user": "Phil",
  "message":"Hello World!"
}'
1 Like

Hi @dadoonet thanks for your quick response. Still I am getting below error.
Could you please assist.

*[root@prestovm1 ~]# curl -XPUT http://localhost:9200/my_index/my_type *
*> --user elastic *
*> -k *
*> -H 'Content-Type: application/json' *
> -d '
> {
> "user": "Phil",
> "message":"Hello World!"
> }'
Enter host password for user 'elastic':
curl: (52) Empty reply from server
[root@prestovm1 ~]#

You did not read correctly my code.
See http vs https.

And please don't format your code with * and/or > but use the </> icon instead.

Initially, it throws an incorrect HTTP method as below.

[root@prestovm1 ~]# curl -XPUT https://localhost:9200/my_index/my_type \
>     --user elastic \
>     -k \
>     -H 'Content-Type: application/json' \
>     -d '
> {
>   "user": "Phil",
>   "message":"Hello World!"
> }'
Enter host password for user 'elastic':
{"error":"Incorrect HTTP method for uri [/my_index/my_type] and method [PUT], allowed: [POST]","status":405}[

Thanks,
Debasis

Replace -XPUT by -XPOST.
And use _doc instead of my_type.

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