POST data from C++

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

    #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:

{"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:

"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

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.

Good luck,
Paul.

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

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

Thank you @mcardlesam

thank you @pjanzen

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