我使用的IDF-4.4.2,使用file_server例程作为http server服务器。然后使用esp_http_client向file_server上传文件,使用的POST上传文件,上报时,报了一个405的警告。Code:
Select all
I (865851) esp_ne
tif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.3W (867401) wifi:idx:3 (ifx:1, 30:c6:f7:23:bd:2c), tid:0, ssn:0, winSize:64I (867471) file_server: Sending file : /config.ini (206 bytes)...I (867471) file_server: File sending completeI (868061) file_server: Receiving file : /text.log...I (868061) file_server: Remaining size : 19I (868081) file_server: File reception completeW (868361) httpd_uri: httpd_uri: Method '3' not allowed for URI '/'W (868361) httpd_txrx: httpd_resp_send_err: 405 Method Not Allowed - Request method for this URI is not handled by server
file_server例程中配置了POST的方法,还出现了警告,应该怎么解决?Code:
Select all
/* URI handler for uploading files to server */ httpd_uri_t file_upload = { .uri = "/upload/*", // Match all URIs of type /upload/path/to/file .method = HTTP_POST, .handler = upload_post_handler, .user_ctx = server_data // Pass server data as context }; httpd_register_uri_handler(server, &file_upload);
esp_http_client端就用了GET和POST两种Code:
Select all
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; /** * NOTE: All the configuration parameters for http_client must be spefied either in URL or as host and path parameters. * If host and path parameters are not set, query parameter will be ignored. In such cases, * query parameter should be specified in URL. * * If URL as well as host and path parameters are specified, values of host and path will be considered. */ esp_http_client_config_t config = { // .host = "httpbin.org", // .path = "/get", // .query = "esp", .url = "http://192.168.4.1/config.ini", .event_handler = _http_event_handler, .user_data = local_response_buffer, // Pass address of local buffer to get response //.disable_auto_redirect = true, }; esp_http_client_handle_t client = esp_http_client_init(&config); // GET esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d", esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err)); } ESP_LOG_BUFFER_HEX(TAG, local_response_buffer, strlen(local_response_buffer)); // POST const char *post_data = "{"field1":"value1"}"; esp_http_client_set_url(client, "http://192.168.4.1/upload/text.log"); esp_http_client_set_method(client, HTTP_METHOD_POST); esp_http_client_set_header(client, "Content-Type", "application/json"); esp_http_client_set_post_field(client, post_data, strlen(post_data)); err = esp_http_client_perform(client); if (err == ESP_OK) { ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d", esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); }