ESResponse ESClient::sendRequest()

in src/ESClient.cpp [39:95]


ESResponse ESClient::sendRequest(String method, String url, String body, String queryString, String contentType){
#ifdef LOGGING
    Serial.println("Sending " + method + " " + url);
    Serial.println("Content-type: " + contentType);
#endif

    if (queryString.length() > 0) {
      url += '?' + queryString;
    }
    client.connectionKeepAlive();
    client.beginRequest();
    if (method == "get") { 
      client.get(url);
    } else if (method == "post") {
      client.post(url);
    } else if (method == "put") {
      client.put(url);
    } else if (method == "delete") {
      client.del(url);
    }

    // Elastic Cloud API key
    if (elasticCloudApiKey.length() > 0) {
      client.sendHeader("Authorization", "ApiKey " + elasticCloudApiKey);
    }
    // Basic authentication
    if (basicUsername.length() > 0 && basicPassword.length() > 0) {
      client.sendBasicAuth(basicUsername, basicPassword);
    }
    // OAuth2 token
    if (oauth2Token.length() > 0) {
      client.sendHeader("Authorization", "Bearer " + oauth2Token);
    }
    // Add x-elastic-client-meta header
    client.sendHeader("x-elastic-client-meta", "arduino=" + version);

    // Add the body, if not empty
    if (body.length() > 0) {
#ifdef LOGGING
      Serial.println("Body: " + body);
#endif
      client.sendHeader("Content-Type", contentType);
      client.sendHeader("Content-Length", body.length());
      client.beginBody();
      client.print(body);
    }
    client.endRequest();

    ESResponse result;
    result.statusCode = client.responseStatusCode();
    result.body = client.responseBody();
#ifdef LOGGING
    Serial.println("Response status code: " + result.statusCode);
    Serial.println("Response body: " + result.body);
#endif    
    return result;
}