# POST data from C++

**URL:** <https://discuss.elastic.co/t/post-data-from-c/233451>\
**Category:** Elasticsearch\
**Created:** [May 20, 2020, 4:22am UTC](https://discuss.elastic.co/t/post-data-from-c/233451 "2020-05-20T04:22:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anowar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anowar/32/74583_2.png) [@Anowar](https://discuss.elastic.co/u/Anowar)\
**Post date:** [May 20, 2020, 4:22am UTC](https://discuss.elastic.co/t/post-data-from-c/233451/1 "2020-05-20T04:22:09Z")

</div>

POST data from C++ using cURL and executing the cURL command by _system()_.

```auto
    #include<iostream>
    #include<string>

    using namespace std;

    int main()
    {      

        system("curl -XPOST \"http://localhost:9200/test/_doc\" -H \"Content-Type: 
        application/json\" -d\"{\"drop\" : 40, \"@timestamp\" : \"2020-05-20T03:05:30\"}\"");

        return 0;

    }

```

It returns the following error:

```auto
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by": {"type":"json_parse_exception","reason":"Unexpected character ('d' (code 100)): was expecting 
double-quote to start field name\n at [Source: (org.elasticsearch.common.bytes.AbstractBytesReference$MarkSupportingStreamInputWrapper); 
line: 1, column: 3]"}},"status":400}

```

The Mapping is:

```auto
"mappings" : {
        "properties" : {
            "drop" : { "type":"long"},
            "@timestamp" : { 
                "type": "date",
                "format": "date_hour_minute_second"
            }
        }
    }
}

```

Btw i am using Ubuntu 18.04 and g++ version is 7.4.0

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [May 20, 2020, 5:11am UTC](https://discuss.elastic.co/t/post-data-from-c/233451/2 "2020-05-20T05:11:27Z")

</div>

Hi,

This is not related to C++, for some reason your command is interpreted incorrectly. I think you need to look closer to your escaping characters. You can have look [at this C++ thread, is might help.](https://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c)

Good luck,  
Paul.

---

<div class="post-metadata">

**Author:** ![mcardlesam](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mcardlesam](https://discuss.elastic.co/u/mcardlesam)\
**Post date:** [May 20, 2020, 7:17am UTC](https://discuss.elastic.co/t/post-data-from-c/233451/3 "2020-05-20T07:17:55Z")

</div>

You are not escaping the json string correctly in the system call. I think its the curl command capturing them?

What you end up sending to the server looks like `{drop : 40, @timestamp : 2020-05-20T03:05:30}`  
Note: there are no quotes around drop - exactly what the error message is telling you.

to fix try using a single quote around your json in the system call like

```auto
 system("curl -XPOST \"http://localhost:9200/test/_doc\" -H \"Content-Type:application/json\" -d \'{\"drop\" : 40, \"@timestamp\" : \"2020-05-20T03:05:30\"}\'");

```

---

<div class="post-metadata">

**Author:** ![Anowar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anowar/32/74583_2.png) [@Anowar](https://discuss.elastic.co/u/Anowar)\
**Post date:** [May 20, 2020, 8:42am UTC](https://discuss.elastic.co/t/post-data-from-c/233451/4 "2020-05-20T08:42:30Z")

</div>

Thank you @mcardlesam

---

<div class="post-metadata">

**Author:** ![Anowar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anowar/32/74583_2.png) [@Anowar](https://discuss.elastic.co/u/Anowar)\
**Post date:** [May 20, 2020, 8:43am UTC](https://discuss.elastic.co/t/post-data-from-c/233451/5 "2020-05-20T08:43:07Z")

</div>

thank you @pjanzen

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 17, 2020, 8:43am UTC](https://discuss.elastic.co/t/post-data-from-c/233451/6 "2020-06-17T08:43:12Z")

</div>

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