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));
}
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"}
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);
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?