How to encode URL query parameters in C++ using Boost.URL
#include <iostream>
#include <boost/url.hpp>
using namespace boost::urls;
int main() {
std::string username = "myusername";
std::string password = "mypassword";
url_view base_url = "https://example.com/api/login";
url encoded_url(base_url);
encoded_url.params().set("user name", username); // With space to test proper encoding
encoded_url.params().set("password", password);
// Prints "https://example.com/api/login?username=my%20user%20name&password=mypassword"
std::cout << "Encoded URL: " << encoded_url << std::endl;
return 0;
}
Compile using
g++ -o urlhost urlhost.cpp -lboost_url