Page 1 of 1

esp_http_client: how to post using json?

Posted: Mon Jul 30, 2018 11:08 pm
by imovit
Hi
I can issue a post action with success but body arrives to rest server as text and then is not well interpreted. I need to send body as json. I did


...
const char *post_data = "{\"channel\":\"A\",\"value\":\"X\"}";
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
//esp_http_client_set_header(client, "Content-Type", "application/json");
esp_err_t err = esp_http_client_perform(client);
...

This works but post_data is sent as text. If I use

...
const char *post_data = "{\"channel\":\"A\",\"value\":\"X\"}";
esp_http_client_set_method(client, HTTP_METHOD_POST);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_err_t err = esp_http_client_perform(client);
...

I see:

Guru Meditation Error: Core 0 panic'ed (LoadProhibited)
. Exception was unhandled.


Why?

In my mind it seems that a a json parser is missing?! I dont know... post_data is already json... Do I need a json parser. If so, I dont hvae any idea how to configure it.

Help please...

Re: esp_http_client: how to post using json?

Posted: Tue Jul 31, 2018 11:32 am
by chegewara
Could you use make monitor and paste full backtace?

Re: esp_http_client: how to post using json?

Posted: Tue Jul 31, 2018 12:44 pm
by imovit
Finally "done"...
I was creating a destroying all the rest things per request. o, the first run well but the other ones do not .
Now, recycling all the things and changing only the post data, it works...
Thanks a lot

Re: esp_http_client: how to post using json?

Posted: Thu Jun 11, 2020 12:35 pm
by Willen
Hi, Imovit,
I met the same problem , could you paste the code for a sample?thank you very much!