Reputation: 78234
I need to create a c++ cgi app the accepts post data. I will be accepting a json object. How to I get the payload?
I can get the get data using the below
int main() {
bool DEBUG = true;
cout << "content-type: text/html" << endl << endl;
//WHAT GOES HERE FOR POST
json=?????
//THIS IS A GET
query_string = getenv("QUERY_STRING");
}
Upvotes: 0
Views: 2470
Reputation: 264331
Assuming apache:
The documentation is found here:
You will find it near the bottom but the post data is provided over stdin.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
bool DEBUG = true;
std::cout << "content-type: text/html\n\n"; // prefer \n\n to std::endl
// you probably don't want to flush immediately.
std::stringstream post;
post << std::cin.rdbuf();
std::cout << "Got: " << post.str() << "\n";
}
Upvotes: 2
Reputation: 2323
If the method type is POST (you may also want to check this) then the POST-data is written to stdin. You can therefore use standard methods like this:
// Do not skip whitespace, more configuration may also be needed.
cin >> noskipws;
// Copy all data from cin, using iterators.
istream_iterator<char> begin(cin);
istream_iterator<char> end;
string json(begin, end);
// Use the JSON data somehow.
cout << "JSON was " << json << endl;
This will read all data from cin into json until an EOF occurs.
Upvotes: 2