ESP32 httpclient - POST JSON - Nested?

aidanvenn
Posts: 1
Joined: Wed Dec 28, 2022 4:06 pm

ESP32 httpclient - POST JSON - Nested?

Postby aidanvenn » Wed Dec 28, 2022 6:39 pm

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

ESP_Sprite
Posts: 9731
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 httpclient - POST JSON - Nested?

Postby ESP_Sprite » Thu Dec 29, 2022 5:46 am

You're not escaping all the double-quotes in that string (" -> \") so the compiler thinks your string ends early.

Who is online

Users browsing this forum: Bing [Bot], ESP_Sprite and 82 guests