Question

I'm developing a web server with Poco library. When my server receive a HTTP request with form data in GET mode, I don't know how to use the class HTMLForm to show a list with received pairs param=value.

With request.getURI().getQuery() I am able to get the complete string. I guess I can split the string in the traditional way, using a tokenizer.

Is there a better way to do it using Poco? Thanks

Was it helpful?

Solution

Ok, class HTMLForm inherits from class NameValueCollection, that implements an iterator useful to move through the pairs "name=value".

This is the code that solve my problem:

string name;
string value;
HTMLForm form( request );

NameValueCollection::ConstIterator i = form.begin();

while(i!=form.end()){

    name=i->first;
    value=i->second;
    cout << name << "=" << value << endl << flush;
    ++i;
}

OTHER TIPS

Using poco version 1.11.0-all (2021-06-28) you can do this:

const Poco::URI Uri(request.getURI());
const Poco::URI::QueryParameters QueryParms = Uri.getQueryParameters();

Poco::URI::QueryParameters is:

std::vector<std::pair<std::string, std::string>>

POCO "NameValueCollection" is almost identical to Vettrasoft Z Directory namevalue_set_o class, which is documented here:

http://www.vettrasoft.com/man/zman-strings-namevalue_set.html

which at least provides some sample code. The biggest problem I have with POCO is lack of examples or explanation on how to use it (including the reference manual pages). For Z Directory's name-value set class, the source code equivalent to that above would look like this:

using namespace std;

int i, ie;
namevalue_set_o nv;
string_o s = "FOO=BAR;DATE=\"12/21/2012\";HOST=vertigo;OSTYPE=\"Windows Vista\"";
nv.load_from_string(s);
i = 0;

while (i < nv.size())
{
  const namevalue_pair_o &item = nv.get(i, &ie);
  if (!ie)
    cout << item.name() << "=" item.value() << endl << flush;
  ++i;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top