File upload using socket succeeds but not with esp_http_client

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

File upload using socket succeeds but not with esp_http_client

Postby amehta » Sat Oct 13, 2018 7:48 pm

Following file upload using socket lib succeeds:

Code: Select all

// Socket opening
	struct sockaddr_in tcpServerAddr;
	tcpServerAddr.sin_addr.s_addr = inet_addr(gateway.ip);
	tcpServerAddr.sin_family = AF_INET;
	tcpServerAddr.sin_port = htons(atoi(PORT));

	s = socket(AF_INET, SOCK_STREAM, 0);
	if (s < 0) {
		ESP_LOGE(TAG, "... Failed to allocate socket.\n");
		vTaskDelay(1000 / portTICK_PERIOD_MS);
		return 1;
	}
	ESP_LOGD(TAG, "... allocated socket\n");
	if (connect(s, (struct sockaddr *) &tcpServerAddr, sizeof(tcpServerAddr))
			!= 0) {
		ESP_LOGE(TAG, "... socket connect failed errno=%d \n", errno);
		close(s);
		return 1;
	}
	ESP_LOGI(TAG, "... connected \n");

	/*
	 * send file logic starts
	 */

	FILE *f = fopen(filename, "r");

	if (f == NULL) {
		ESP_LOGE(TAG, "Failed to open file for sending");
		return 1;
	}

	bzero(recv_buf, sizeof(recv_buf));

	while ((r = fread(&recv_buf[0], sizeof(char), 512, f)) > 0) {
		if (x == 0) {
			if (send(s, s2, r, 0) < 0) {
				printf("ERROR: Failed to send file %s.\n", filename);
				break;
			}
			x = 1;
		}
		if (send(s, &recv_buf[0], r, 0) < 0) {
			printf("ERROR: Failed to send file %s.\n", filename);
			break;
		}
		bzero(recv_buf, sizeof(recv_buf));
	}
Output for above

Code: Select all

*********REQUEST**********
I (50537) abc.utils.networking: 
Request sent : 
POST /logs/api/submit HTTP/1.0
Host: 192.168.2.2:80
User-Agent: abc-Node
Content-Length: 3040
Content-Disposition: attachment; filename=chip.log


----- File Content -----

**********RESPONSE***************
HTTP/1.1 200 OK
Date: Sat, 13 Oct 2018 17:43:47 GMT
Server: Apache/2.4.25 (Raspbian)
Content-Length: 20
X-Frame-Options: SAMEORIGIN
Allow: POST, OPTIONS
Vary: Accept,Cookie
Connection: close
Content-Type: application/json

{"status":"SUCCESS"}
Following file upload code using esp_http_client fails

Code: Select all

FILE *f = fopen(filename, "r");
	if (f == NULL) {
		ESP_LOGE(TAG, "Failed to open file for sending");
		return -1;
	}

	esp_http_client_handle_t client = esp_http_client_init(&config);

	esp_http_client_set_header(client, "User-Agent", "abc-Node");
	esp_http_client_set_header(client, "Content-Disposition", "attachment; filename=chip.log");
	esp_http_client_set_url(client, url);
	esp_http_client_set_method(client, HTTP_METHOD_POST);

	esp_err_t err;
	if ((err = esp_http_client_open(client, size)) != ESP_OK) {
		ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
		free(buffer);
		return -1;
	}

	bzero(buffer, MAX_HTTP_RECV_BUFFER);
	printf("Total size : %d\n", size);

	while ((ret = fread(&buffer[0], sizeof(char), MAX_HTTP_RECV_BUFFER, f)) > 0) {
		esp_http_client_write(client, buffer, MAX_HTTP_RECV_BUFFER);
		bzero(buffer, MAX_HTTP_RECV_BUFFER);
	}
	int content_length =  esp_http_client_fetch_headers(client);
	int total_read_len = 0, read_len;
	if (total_read_len < content_length && content_length <= MAX_HTTP_RECV_BUFFER) {
		read_len = esp_http_client_read(client, buffer, content_length);
		if (read_len <= 0) {
			ESP_LOGE(TAG, "Error read data");
		}
		buffer[read_len] = 0;
		ESP_LOGI(TAG, "read_len = %d", read_len);
	}
	ESP_LOGI(TAG, "HTTP Status = %d, content_length = %d",
	                    esp_http_client_get_status_code(client),
	                    esp_http_client_get_content_length(client));
	esp_http_client_close(client);
	esp_http_client_cleanup(client);
	free(buffer);
	fclose(f);
Response for above request:

Code: Select all

*********REQUEST**************
I (88337) abc.utils.networking: 
Request sent : 
POST /logs/api/submit HTTP/1.0
Host: 192.168.2.2:80
User-Agent: abc-Node
Content-Length: 2838
Content-Disposition: attachment; filename=chip.log

---- File-Content ----

******** Response*******

Date: Sat, 13 Oct 2018 19:13:01 GMT
Server: Apache/2.4.25 (Raspbian)
Content-Length: 20
X-Frame-Options: SAMEORIGIN
Allow: POST, OPTIONS
Vary: Cookie
Connection: close
Content-Type: application/json

{"errmsg": "KeyError 'file'"}
I (88851) abc.utils.networking: read_len = 29
I (88851) abc.utils.networking: HTTP Status = 500, content_length = 29


Can anyone point out what could be wrong with my second request?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: File upload using socket succeeds but not with esp_http_client

Postby WiFive » Sat Oct 13, 2018 10:16 pm

Code: Select all

esp_http_client_write(client, buffer, MAX_HTTP_RECV_BUFFER);

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: File upload using socket succeeds but not with esp_http_client

Postby amehta » Sun Oct 14, 2018 3:41 am

I am using this API in loop to send file after using "esp_client_open". Still it is getting failed!
WiFive wrote:

Code: Select all

esp_http_client_write(client, buffer, MAX_HTTP_RECV_BUFFER);

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: File upload using socket succeeds but not with esp_http_client

Postby WiFive » Sun Oct 14, 2018 4:51 am

Code: Select all

esp_http_client_write(client, buffer, ret);

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: File upload using socket succeeds but not with esp_http_client

Postby amehta » Sun Oct 14, 2018 5:40 am

It is still failing with the same error. But I agree it should have been "ret" (size returned from the fread function) instead of MAX_HTTP_RECV_BUFFER.
WiFive wrote:

Code: Select all

esp_http_client_write(client, buffer, ret);

Anything else that looks wrong??

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: File upload using socket succeeds but not with esp_http_client

Postby amehta » Tue Oct 16, 2018 3:08 pm

@WiFive any more suggestions on this?

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: File upload using socket succeeds but not with esp_http_client

Postby fly135 » Tue Oct 16, 2018 4:21 pm

Your file appears to have changed size from 3040 to 2838 (i.e. Content-Length). Maybe that's a clue.

John A

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: File upload using socket succeeds but not with esp_http_client

Postby amehta » Tue Oct 16, 2018 4:57 pm

Its a log file, so new logs are appended every time and they are being sent constantly to a local server. So thats the reason the file size is changing.

lizhe1988
Posts: 2
Joined: Fri Apr 12, 2019 7:25 pm

Re: File upload using socket succeeds but not with esp_http_client

Postby lizhe1988 » Tue May 14, 2019 11:51 am

Hi, Did you complete the file upload function? :)

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: File upload using socket succeeds but not with esp_http_client

Postby amehta » Wed May 15, 2019 8:38 am

yes I have completed the file upload function. Let me know if you need help.

Who is online

Users browsing this forum: No registered users and 84 guests