Merge branch 'bugfix/fix_http_client_header_bug' into 'master'

esp_http_client: Fix content-type header overwritten by esp_http_client_set_post_field

See merge request idf/esp-idf!2552
This commit is contained in:
Angus Gratton 2018-06-21 08:10:13 +08:00
commit b47c8fcbf7
2 changed files with 32 additions and 5 deletions

View File

@ -258,6 +258,11 @@ esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char
return http_header_set(client->request->headers, key, value);
}
esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value)
{
return http_header_get(client->request->headers, key, value);
}
esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const char *key)
{
return http_header_delete(client->request->headers, key);
@ -952,7 +957,13 @@ esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const
client->post_len = len;
ESP_LOGD(TAG, "set post file length = %d", len);
if (client->post_data) {
err = esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
char *value = NULL;
if ((err = esp_http_client_get_header(client, "Content-Type", &value)) != ESP_OK) {
return err;
}
if (value == NULL) {
err = esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
}
} else {
client->post_len = 0;
err = esp_http_client_set_header(client, "Content-Type", NULL);

View File

@ -168,7 +168,7 @@ esp_err_t esp_http_client_perform(esp_http_client_handle_t client);
esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url);
/**
* @brief Set post data, this function must be called before `esp_http_client_finalize_open` or perform
* @brief Set post data, this function must be called before `esp_http_client_perform`.
* Note: The data parameter passed to this function is a pointer and this function will not copy the data
*
* @param[in] client The esp_http_client handle
@ -205,6 +205,22 @@ int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data)
*/
esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value);
/**
* @brief Get http request header.
* The value parameter will be set to NULL if there is no header which is same as
* the key specified, otherwise the address of header value will be assigned to value parameter.
* This function must be called after `esp_http_client_init`.
*
* @param[in] client The esp_http_client handle
* @param[in] key The header key
* @param[out] value The header value
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value);
/**
* @brief Set http request method
*
@ -266,7 +282,7 @@ int esp_http_client_fetch_headers(esp_http_client_handle_t client);
/**
* @brief Check response data is chunked, must call after `esp_http_client_finalize_open`
* @brief Check response data is chunked
*
* @param[in] client The esp_http_client handle
*
@ -289,7 +305,7 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
/**
* @brief Get http response status code, the valid value if this function invoke after `esp_http_client_perform` or `esp_http_client_finalize_open`
* @brief Get http response status code, the valid value if this function invoke after `esp_http_client_perform`
*
* @param[in] client The esp_http_client handle
*
@ -299,7 +315,7 @@ int esp_http_client_get_status_code(esp_http_client_handle_t client);
/**
* @brief Get http response content length (from header Content-Length)
* the valid value if this function invoke after `esp_http_client_perform` or `esp_http_client_finalize_open`
* the valid value if this function invoke after `esp_http_client_perform`
*
* @param[in] client The esp_http_client handle
*