simple HTTP post support (urlencode parameters)

This commit is contained in:
Benjamin Sergeant
2019-02-25 15:55:38 -08:00
parent c04bc3cdfc
commit 3bcd6f97a6
10 changed files with 164 additions and 47 deletions

View File

@ -17,7 +17,8 @@
namespace ix
{
int ws_http_client_main(const std::string& url);
int ws_http_client_main(const std::string& url,
const std::string& data);
int ws_ping_pong_main(const std::string& url);
@ -47,11 +48,13 @@ int main(int argc, char** argv)
std::string url("ws://127.0.0.1:8080");
std::string path;
std::string user;
std::string data;
int port = 8080;
CLI::App* sendApp = app.add_subcommand("send", "Send a file");
sendApp->add_option("url", url, "Connection url")->required();
sendApp->add_option("path", path, "Path to the file to send")->required();
sendApp->add_option("path", path, "Path to the file to send")
->required()->check(CLI::ExistingPath);
CLI::App* receiveApp = app.add_subcommand("receive", "Receive a file");
receiveApp->add_option("url", url, "Connection url")->required();
@ -77,6 +80,8 @@ int main(int argc, char** argv)
CLI::App* httpClientApp = app.add_subcommand("http_client", "HTTP Client");
httpClientApp->add_option("url", url, "Connection url")->required();
httpClientApp->add_option("-d", data, "Form data")->join();
httpClientApp->add_option("-F", data, "Form data")->join();
CLI11_PARSE(app, argc, argv);
@ -117,7 +122,8 @@ int main(int argc, char** argv)
}
else if (app.got_subcommand("http_client"))
{
return ix::ws_http_client_main(url);
std::cout << "data: " << data << std::endl;
return ix::ws_http_client_main(url, data);
}
return 1;

View File

@ -7,7 +7,7 @@
//
// Simple chat program that talks to the node.js server at
// websocket_chat_server/broacast-server.js
//
//
#include <iostream>
#include <sstream>
#include <queue>

View File

@ -10,11 +10,51 @@
namespace ix
{
void ws_http_client_main(const std::string& url)
//
// Useful endpoint to test HTTP post
// https://postman-echo.com/post
//
HttpParameters parsePostParameters(const std::string& data)
{
HttpParameters httpParameters;
// Split by ;
std::string token;
std::stringstream tokenStream(data);
while (std::getline(tokenStream, token))
{
std::size_t pos = token.rfind('=');
// Bail out if last '.' is found
if (pos == std::string::npos) continue;
auto key = token.substr(0, pos);
auto val = token.substr(pos+1);
std::cout << key << ": " << val << std::endl;
httpParameters[key] = val;
}
return httpParameters;
}
int ws_http_client_main(const std::string& url,
const std::string& data)
{
HttpParameters httpParameters = parsePostParameters(data);
HttpClient httpClient;
bool verbose = true;
auto out = httpClient.get(url, verbose);
HttpResponse out;
if (data.empty())
{
out = httpClient.get(url, verbose);
}
else
{
out = httpClient.post(url, httpParameters, verbose);
}
auto errorCode = std::get<0>(out);
auto headers = std::get<1>(out);
auto payload = std::get<2>(out);
@ -32,5 +72,7 @@ namespace ix
}
std::cout << "payload: " << payload << std::endl;
return 0;
}
}