Page 1 of 1

ESP32 httpclient - POST JSON - Nested?

Posted: Wed Dec 28, 2022 6:39 pm
by aidanvenn
Hi,

Just started playing with a ESP32. Enjoyable so far.. :D

I work in networks and we use www.zabbix.com to monitor routers etc.

Zabbix comes with a HTTP / JSON API.

When I post using:
int httpResponseCode = http.POST("{\"jsonrpc\":\"2.0\",\"method\":\"apiinfo.version\",\"params\":\[]\,\"id\":\"1\"}");
for JSON:

Code: Select all

{
    "jsonrpc": "2.0",
    "method": "apiinfo.version",
    "params": [],
    "id": 1
}
It works fine.

However when I post:
int httpResponseCode = http.POST("{\"jsonrpc\":\"2.0\",\"method\":\"item.update\",\"params\":\{"itemid":"42244","status": 0},"auth":\"xxxxxx\",\"id\":\"1\"}");
for JSON:

Code: Select all

{
    "jsonrpc": "2.0",
    "method": "item.update",
    "params": {
        "itemid": "42244",
        "status": 0
    },
    "auth": "xxxxxx",
    "id": 1
}
I get:
int httpResponseCode = http.POST("{\"jsonrpc\":\"2.0\",\"method\":\"item.update\",\"params\":\{"itemid":"42244","status": 0},"auth":\"xxxxxx\",\"id\":\"1\"}"); ^~~
exit status 1
Compilation error: unable to find string literal operator 'operator""itemid' with 'const char [52]', 'unsigned int' arguments
It appears to be how I am presenting the (nested?) JSON in the parameters section(?). Tried many ways but research such as google: esp32 httpclient unable to find string literal operator 'operator has not been successful.

Any ideas or pointers to the correct answer?

Many thanks

Aidan

Re: ESP32 httpclient - POST JSON - Nested?

Posted: Thu Dec 29, 2022 5:46 am
by ESP_Sprite
You're not escaping all the double-quotes in that string (" -> \") so the compiler thinks your string ends early.