Page 1 of 1

HTTP POST does not work in esp-idf, it does work in arduino..

Posted: Tue Jan 01, 2019 7:18 pm
by SuperTechSaiyan3
Hey all,

To start I have used the search function and read through the problems from other posts, the most similar one was this https://www.esp32.com/viewtopic.php?t=843 tried the same thing but it seemed like an old example and couldn't get it to work.

My issue is that I am working through http_request_example, and I change the code to suit my needs and it connect to my website easily, but I get a HTTP 400 error, and a esp errno of 128 (I believe is SSL?)

here is what I am sending as the post request:

Code: Select all

POST http://xxx.com/xxx/Device04/Test2.php HTTP/1.1
Host: xxx.com
User-Agent: ESP32HTTPClient
Connection: close
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
Content-Type: application/x-www-form-urlencoded
Content-Length: 58

t=10&h=10&t2=10&s=(10,10)&password=Password&table=Device05
here is output from IDF

Code: Select all

I (561465) Test: Connected to AP
I (561465) Test: DNS lookup succeeded. IP=
I (561465) Test: ... allocated socket
I (561505) Test: ... connected
I (561505) Test: ... socket send success
I (561505) Test: ... set socket receiving timeout success
HTTP/1.1 400 Bad Request
Server: nginx/1.14.1
Date: Tue, 01 Jan 2019 08:00:44 GMT
Content-Type: text/html
Content-Length: 173
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.14.1</center>
</body>
</html>
I (561565)Test: ... done reading from socket. Last read return=0 errno=128
any help will be greatly appreciated. I am also not using https and my site does not have ssl currently. as mentioned in title, it works fine in arduino, and GET works as well.

Got it to work

Posted: Thu Jan 03, 2019 5:12 am
by SuperTechSaiyan3
good news I ended up getting the code to finally work, after working through it when I can the past couple of days...

In case you come to this issue:

Here what I changed:
I changed

Code: Select all

 http://www.xxx.com/aaa/bbb/ccc.php
to

Code: Select all

 [/aaa/bbb/ccc]
removing my url + .com from the first line of the POST ended up working beautifully! This is confusing since in the past I had to add the http:// in previous esp-idf versions which was weird...

here is what the final POST looked like:

Code: Select all

 
POST /xxx/xxx/Test2.php HTTP/1.1                                                                                                                                                       
Host: xxx.com                                                                                                                                                                         
User-Agent: ESP32HTTPClient                                                                                                                                                                 
Connection: close                                                                                                                                                                           
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0                                                                                                                                           
Content-Type: application/x-www-form-urlencoded                                                                                                                                             
Content-Length: 58                                                                                                                                                                          
                                                                                                                                                                                            
t=30&h=10&t2=10&s=(10,10)&password=Password&table=Device05 


Re: HTTP POST does not work in esp-idf, it does work in arduino..

Posted: Thu Jan 03, 2019 6:03 am
by chegewara
Works for me:

Code: Select all

	esp_http_client_handle_t client = esp_http_client_init(&config);
	esp_http_client_set_method(client, HTTP_METHOD_POST);
	esp_http_client_set_header(client, "Content-Type", "image/jpeg");
	esp_http_client_set_post_field(client, (const char*)fb_data->base64_data, fb_data->base64_size);
	esp_err_t err = esp_http_client_perform(client);
	if (err == ESP_OK) {
		fb_data->resp_len = esp_http_client_get_content_length(client);
		ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
			esp_http_client_get_status_code(client),
			fb_data->resp_len);
	}
	else {
		ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
	}

Re: HTTP POST does not work in esp-idf, it does work in arduino..

Posted: Fri Jan 04, 2019 5:25 am
by SuperTechSaiyan3
chegewara,

Interesting, did my solution work, or my initial post worked for you? do you mind sending me what your POST request looked like?


thanks!

Re: HTTP POST does not work in esp-idf, it does work in arduino..

Posted: Fri Jan 04, 2019 6:35 pm
by chegewara
I didnt try with your solution, above code snippet is from my app.