request methods support in HTTPClient v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 19 Mar 2022 00:49:47 +0100
branchv_0
changeset 14 e41abdd36ff1
parent 13 18f4f7e93c48
child 15 25be376736cc
request methods support in HTTPClient
src/HTTPClient.cpp
--- a/src/HTTPClient.cpp	Tue Mar 15 23:58:27 2022 +0100
+++ b/src/HTTPClient.cpp	Sat Mar 19 00:49:47 2022 +0100
@@ -15,6 +15,7 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <string>
 #include <sstream>
 #include <iostream>
 
@@ -130,7 +131,17 @@
 const HTTPClient::Response HTTPClient::exchange(const Request& request) {
 	HTTPClient::Response response;
 
-	// TODO: set request method
+	// set request method
+	std::string method;
+	if (request.method == Method::DELETE) method = "DELETE";
+	else if (request.method == Method::GET) method = "GET";
+	else if (request.method == Method::HEAD) method = "HEAD";
+	else if (request.method == Method::PATCH) method = "PATCH";
+	else if (request.method == Method::POST) method = "POST";
+	else if (request.method == Method::PUT) method = "PUT";
+	else throw std::invalid_argument("Unsupported HTTP method: " + std::to_string((int) request.method));
+	curl_easy_setopt(impl->curl, CURLOPT_CUSTOMREQUEST, method.c_str());
+	if (request.method == Method::HEAD) curl_easy_setopt(impl->curl, CURLOPT_NOBODY, 1L);
 
 	// set URL
 	curl_easy_setopt(impl->curl, CURLOPT_URL, request.url.c_str());