Skip to content

Commit 22b8e99

Browse files
committed
post follow
1 parent d075a52 commit 22b8e99

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/http_client_extension.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ static void HTTPPostRequestFunction(DataChunk &args, ExpressionState &state, Vec
105105
D_ASSERT(args.data.size() == 3);
106106

107107
auto &url_vector = args.data[0];
108-
auto &headers_vector = args.data[1];
109-
auto &body_vector = args.data[2];
108+
auto &headers_vector = args.data[1]; // Already passed as a serialized string
109+
auto &body_vector = args.data[2]; // Already passed as a JSON string
110110

111+
// Use TernaryExecutor instead of UnaryExecutor
111112
TernaryExecutor::Execute<string_t, string_t, string_t, string_t>(
112113
url_vector, headers_vector, body_vector, result, args.size(),
113-
[&](string_t url, string_t headers, string_t body) {
114+
[&](string_t url, string_t headers_varchar, string_t body_varchar) {
114115
std::string url_str = url.GetString();
115116

116117
// Parse the URL to extract the domain and path
@@ -130,20 +131,23 @@ static void HTTPPostRequestFunction(DataChunk &args, ExpressionState &state, Vec
130131
path = "/";
131132
}
132133

133-
// Create the client and set a timeout (e.g., 10 seconds)
134+
// Create the client and set a timeout
134135
duckdb_httplib_openssl::Client client(domain.c_str());
135136
client.set_read_timeout(10, 0); // 10 seconds
136137

137-
// Prepare headers
138+
// Follow redirects for POST as well
139+
client.set_follow_location(true);
140+
141+
// Deserialize the header string into a header map
138142
duckdb_httplib_openssl::Headers header_map;
139-
std::istringstream header_stream(headers.GetString());
143+
std::istringstream header_stream(headers_varchar.GetString());
140144
std::string header;
141145
while (std::getline(header_stream, header)) {
142146
size_t colon_pos = header.find(':');
143147
if (colon_pos != std::string::npos) {
144148
std::string key = header.substr(0, colon_pos);
145149
std::string value = header.substr(colon_pos + 1);
146-
// Trim leading and trailing whitespace
150+
// Trim leading/trailing whitespace
147151
key.erase(0, key.find_first_not_of(" \t"));
148152
key.erase(key.find_last_not_of(" \t") + 1);
149153
value.erase(0, value.find_first_not_of(" \t"));
@@ -152,8 +156,11 @@ static void HTTPPostRequestFunction(DataChunk &args, ExpressionState &state, Vec
152156
}
153157
}
154158

155-
// Make the POST request with headers and body
156-
auto res = client.Post(path.c_str(), header_map, body.GetString(), "application/json");
159+
// Prepare the POST body (it is passed as a string)
160+
std::string body_str = body_varchar.GetString();
161+
162+
// Make the POST request
163+
auto res = client.Post(path.c_str(), header_map, body_str, "application/json");
157164
if (res) {
158165
if (res->status == 200) {
159166
return StringVector::AddString(result, res->body);
@@ -163,8 +170,6 @@ static void HTTPPostRequestFunction(DataChunk &args, ExpressionState &state, Vec
163170
} else {
164171
// Handle the error case
165172
std::string err_message = "HTTP POST request failed. ";
166-
167-
// Convert httplib error codes to a descriptive message
168173
switch (res.error()) {
169174
case duckdb_httplib_openssl::Error::Connection:
170175
err_message += "Connection error.";

0 commit comments

Comments
 (0)