乐鑫技术交流
直播中

乐骞添

7年用户 996经验值
私信 关注
[问答]

使用file_server例程作为http server服务器,报了一个405警告的原因?

我使用的IDF-4.4.2,使用file_server例程作为http server服务器。然后使用esp_http_client向file_server上传文件,使用的POST上传文件,上报时,报了一个405的警告。
Code: Select all
I (865851) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.3
W (867401) wifi:idx:3 (ifx:1, 30:c6:f7:23:bd:2c), tid:0, ssn:0, winSize:64
I (867471) file_server: Sending file : /config.ini (206 bytes)...
I (867471) file_server: File sending complete
I (868061) file_server: Receiving file : /text.log...
I (868061) file_server: Remaining size : 19
I (868081) file_server: File reception complete
W (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));
    }

更多回帖

发帖
×
20
完善资料,
赚取积分