Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpd_resp_send_chunk() to send JSON data issue (IDFGH-14548) #15309

Closed
pavel808 opened this issue Jan 30, 2025 · 3 comments
Closed

httpd_resp_send_chunk() to send JSON data issue (IDFGH-14548) #15309

pavel808 opened this issue Jan 30, 2025 · 3 comments
Assignees
Labels
Awaiting Response awaiting a response from the author Resolution: Won't Do This will not be worked on Status: Done Issue is done internally

Comments

@pavel808
Copy link

pavel808 commented Jan 30, 2025

I have a HTTP server running on an ESP32-S3 device. I can receive the POST requests via CURL fine, but I am having issues sending back a JSON response string as below. test_json_data. CURL just sits there blocked and nothing gets received. What exactly am I doing wrong here ? Thanks. This is the curl command from the client :

curl -X POST --data "volume_up" edge-server.local:83/sendCommand -v

It causes the below my_post_handler function to get invoked, but problems getting simple JSON data back to the client.

static esp_err_t my_post_handler(httpd_req_t *req)
{
   ESP_LOGI(TAG, "my_post_handler");

    char buf[60];
    int ret = 0;
	int remaining = req->content_len;

    esp_err_t err = ESP_OK;

    httpd_resp_set_type(req, "application/json");
    httpd_resp_set_hdr(req, "Connection", "keep-alive");
    httpd_resp_set_status(req, HTTPD_200);

    while (remaining > 0)
    {
        /* Read the data for the request */
        if ((ret = httpd_req_recv(req, buf,
                        MIN(remaining, sizeof(buf)))) <= 0)
        {
            if (ret == HTTPD_SOCK_ERR_TIMEOUT)
            {
                /* Retry receiving if timeout occurred */
                continue;
            }

            err = ESP_FAIL;
        }

        if (err == ESP_OK)
        {
        	
		remaining -= ret;

		/* Log data received */
		ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
		ESP_LOGI(TAG, "%.*s", ret, buf);
		ESP_LOGI(TAG, "====================================");

		char delimiter = '/';
		std::string tmp = "";

		std::string fullcmd(buf, buf + req->content_len);
		ESP_LOGI(TAG, "fullcmd : %s ", fullcmd.c_str());
		std::vector<std::string> parts;

		std::istringstream iss(fullcmd);
		while(std::getline(iss, tmp, delimiter))
		{
			ESP_LOGI(TAG, "PART : %s", tmp.c_str());
			parts.push_back(tmp);
		}

		if (parts.size() >=  3)
		{
			err = process_data(parts);

			if (err != ESP_OK)
			{
				ESP_LOGE(TAG, "ERROR! Unable to process data");
				err = ESP_FAIL;
			}
		}
		else
		{
			err = ESP_FAIL;
		}
        }
    }

    if (err != ESP_OK)
    {
    	httpd_resp_set_status(req, HTTPD_500);
    }

    memset(buf, 0, sizeof(buf));
    const char *test_json_data = "{\"channel\":\"A\",\"value\":\"X\"}";
    strncpy(buf, test_json_data, sizeof(buf));

    httpd_resp_send_chunk(req, buf, ret);

    return err;
}
@pavel808 pavel808 changed the title httpd_resp_send_chunk to send JKSON data issue httpd_resp_send_chunk() to send JSON data issue Jan 30, 2025
@espressif-bot espressif-bot added the Status: Opened Issue is new label Jan 30, 2025
@github-actions github-actions bot changed the title httpd_resp_send_chunk() to send JSON data issue httpd_resp_send_chunk() to send JSON data issue (IDFGH-14548) Jan 30, 2025
@chegewara
Copy link
Contributor

You need this at the end
httpd_resp_send_chunk(req, NULL, 0);

@hrushikesh430
Copy link
Collaborator

Hi @pavel808, Could you please send your sdkconfig and patch file. So that It will help me to reproduce the issue easily. Even some of the functions like process_data(), send me the file where they are defined.

@espressif-bot espressif-bot added the Awaiting Response awaiting a response from the author label Feb 21, 2025
@hrushikesh430
Copy link
Collaborator

Hello @pavel808, Closing this issue due to response delay. Please feel free to reopen the issue for further query. Thank you.

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: Won't Do This will not be worked on and removed Status: In Progress Work is in progress labels Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Response awaiting a response from the author Resolution: Won't Do This will not be worked on Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

4 participants