Http client and C++
Posted: Wed Mar 27, 2019 7:58 am
Hi guys!
I'm trying to write a http client using C++ over the Esp-idf http client.
For now I have only one static method to make post requests and one request type: JsonRequest.
Request class definition:
Http client definition
Http client implementation:
For now a have a problem with url setting and client initialization:
make monitor tell me:
Everything goes OK.
I tried to debug "request->getUrl().c_str()" with ESP_LOG - url was correct.
What can be wrong with that?
I'm trying to write a http client using C++ over the Esp-idf http client.
For now I have only one static method to make post requests and one request type: JsonRequest.
Request class definition:
Code: Select all
class JsonRequest: public HttpRequestInterface {
private:
std::string url;
std::vector<HttpHeader> headers;
std::vector<HttpParam> params;
public:
JsonRequest(std::string url);
void addHeader(HttpHeader header);
void addParam(HttpParam param);
std::vector<HttpHeader> getHeaders();
std::string getUrl();
std::string paramsToString();
};
Code: Select all
class HttpClient {
private:
static HttpResponse * doRequest(esp_http_client_handle_t client, HttpRequestInterface * request);
public:
static HttpResponse * post(HttpRequestInterface * request);
};
Code: Select all
ESP32::HttpResponse * ESP32::HttpClient::post(ESP32::HttpRequestInterface * request) {
esp_http_client_config_t config = {
.url = request->getUrl().c_str(),
.method = HTTP_METHOD_POST
};
esp_http_client_handle_t client = esp_http_client_init(&config);
std::string params = request->paramsToString();
esp_http_client_set_post_field(client, params.c_str(), params.size());
return ESP32::HttpClient::doRequest(client, request);
}
ESP32::HttpResponse * ESP32::HttpClient::doRequest(esp_http_client_handle_t client, ESP32::HttpRequestInterface * request) {
std::vector<ESP32::HttpHeader>::iterator it;
for(it = request->getHeaders().begin(); it != request->getHeaders().end(); it++ ) {
esp_http_client_set_header(client, it->name.c_str(), it->value.c_str());
}
esp_err_t err;
if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
// exception
}
int content_length = esp_http_client_fetch_headers(client);
char *buffer = (char *) malloc(content_length + 1);
int total_read_len = 0, read_len;
if (total_read_len < content_length) {
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_LOGD(TAG, "read_len = %d", read_len);
}
esp_http_client_close(client);
esp_http_client_cleanup(client);
ESP32::HttpResponse * response = new ESP32::HttpResponse(
std::string(buffer),
esp_http_client_get_status_code(client)
);
free(buffer);
return response;
}
Code: Select all
esp_http_client_config_t config = {
.url = request->getUrl().c_str(),
....
};
esp_http_client_handle_t client = esp_http_client_init(&config);
But if i set client url as:E (2675) HTTP_CLIENT: Error parse url \��?
E (2675) HTTP_CLIENT: Error while setting default configurations
Code: Select all
esp_http_client_config_t config = {
.url = "http://someDomain.com",
....
};
I tried to debug "request->getUrl().c_str()" with ESP_LOG - url was correct.
What can be wrong with that?