Skip to content

Commit fcf47a7

Browse files
authored
乐鑫添加http body 分隔符
1 parent d647071 commit fcf47a7

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

examples/at_custom_cmd/custom/at_custom_cmd.c

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ static esp_err_t at_http_event_handler(esp_http_client_event_t *evt)
506506
memcpy(data + header_len + evt->data_len, "\r\n", 2);
507507
esp_at_port_write_data(data, header_len + evt->data_len + 2);
508508
free(data);
509+
printf("\r\n%.*s\r\n", evt->data_len, (char *)evt->data);
509510
break;
510511
default:
511512
break;
@@ -519,6 +520,7 @@ static uint8_t at_setup_cmd_fs_to_http_server(uint8_t para_num)
519520
esp_err_t ret = ESP_OK;
520521
int32_t cnt = 0, url_len = 0;
521522
uint8_t *dst_path = NULL, *data = NULL;
523+
char *body_start = NULL, *body_end = NULL;
522524

523525
// dst file path
524526
if (esp_at_get_para_as_str(cnt++, &dst_path) != ESP_AT_PARA_PARSE_RESULT_OK) {
@@ -589,14 +591,40 @@ static uint8_t at_setup_cmd_fs_to_http_server(uint8_t para_num)
589591
goto cmd_exit;
590592
}
591593
esp_http_client_set_method(sp_fs_to_http->client, HTTP_METHOD_POST);
592-
esp_http_client_set_header(sp_fs_to_http->client, "Content-Type", "multipart/form-data;--ESP32Boundary123456");
594+
// esp_http_client_set_header(sp_fs_to_http->client, "Content-Type", "multipart/form-data");
595+
596+
// set new header
597+
char *boundary = "--myboundary";
598+
char value[128];
599+
snprintf(value, 128, "multipart/form-data; boundary=--%s", boundary);
600+
esp_http_client_set_header(sp_fs_to_http->client, "Content-Type", value);
601+
602+
// construct http body start and end
603+
int rlen = 0;
604+
body_start = calloc(1, 512);
605+
body_end = calloc(1, 64);
606+
if (!body_start || !body_end) {
607+
ret = ESP_ERR_NO_MEM;
608+
goto cmd_exit;
609+
}
610+
int start_len = snprintf(body_start, 512,
611+
"----%s\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nAlice\r\n----%s\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n",
612+
boundary, boundary, sp_fs_to_http->fs_handle->path);
613+
int end_len = snprintf(body_end, 512, "\r\n----%s--\r\n", boundary);
593614

594615
// establish http connection
595-
ret = esp_http_client_open(sp_fs_to_http->client, sp_fs_to_http->fs_handle->total_size);
616+
ret = esp_http_client_open(sp_fs_to_http->client, sp_fs_to_http->fs_handle->total_size + start_len + end_len);
596617
if (ret != ESP_OK) {
597618
goto cmd_exit;
598619
}
599620

621+
rlen = esp_http_client_write(sp_fs_to_http->client, body_start, start_len);
622+
if (rlen != start_len) {
623+
ESP_LOGE(TAG_POST, "esp_http_client_write() failed");
624+
ret = ESP_FAIL;
625+
goto cmd_exit;
626+
}
627+
600628
// post file to remote server
601629
data = (uint8_t *)malloc(AT_HEAP_BUFFER_SIZE);
602630
if (!data) {
@@ -629,6 +657,13 @@ static uint8_t at_setup_cmd_fs_to_http_server(uint8_t para_num)
629657
}
630658
} while (1);
631659

660+
rlen = esp_http_client_write(sp_fs_to_http->client, body_end, end_len);
661+
if (rlen != end_len) {
662+
ESP_LOGE(TAG_POST, "esp_http_client_write() failed");
663+
ret = ESP_FAIL;
664+
goto cmd_exit;
665+
}
666+
632667
// post over
633668
if (ret != ESP_OK) {
634669
ESP_LOGE(TAG_POST, "total expected len:%d, but total post size:%d", sp_fs_to_http->fs_handle->total_size, sp_fs_to_http->post_size);
@@ -660,6 +695,12 @@ static uint8_t at_setup_cmd_fs_to_http_server(uint8_t para_num)
660695
if (data) {
661696
free(data);
662697
}
698+
if (body_start) {
699+
free(body_start);
700+
}
701+
if (body_end) {
702+
free(body_end);
703+
}
663704
// clean resources
664705
at_fs_to_http_clean();
665706
if (ret != ESP_OK) {
@@ -668,7 +709,7 @@ static uint8_t at_setup_cmd_fs_to_http_server(uint8_t para_num)
668709
}
669710

670711
return ESP_AT_RESULT_CODE_OK;
671-
}
712+
}
672713

673714
/****************************************************************************************************************************************/
674715

0 commit comments

Comments
 (0)