Advertisement
Guest User

Returning JSON object to HTTP POST REST call from C++

a guest
Jun 27th, 2012
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. int main(void) {
  2.     // Backup the stdio streambufs
  3.     streambuf * cin_streambuf  = cin.rdbuf();
  4.     streambuf * cout_streambuf = cout.rdbuf();
  5.     streambuf * cerr_streambuf = cerr.rdbuf();
  6.  
  7.     FCGX_Request request;
  8.  
  9.     FCGX_Init();
  10.     FCGX_InitRequest(&request, 0, 0);
  11.  
  12.     while (FCGX_Accept_r(&request) == 0) {
  13.         fcgi_streambuf cin_fcgi_streambuf(request.in);
  14.         fcgi_streambuf cout_fcgi_streambuf(request.out);
  15.         fcgi_streambuf cerr_fcgi_streambuf(request.err);
  16.  
  17.         cin.rdbuf(&cin_fcgi_streambuf);
  18.         cout.rdbuf(&cout_fcgi_streambuf);
  19.         cerr.rdbuf(&cerr_fcgi_streambuf);
  20.  
  21.         const char * uri = FCGX_GetParam("REQUEST_URI", request.envp);
  22.  
  23.         string content = get_request_content(request);
  24.  
  25.         if (content.length() == 0) {
  26.             content = ", World!";
  27.         }
  28.     printcontent(content);
  29.    }
  30.  
  31.     // restore stdio streambufs
  32.     cin.rdbuf(cin_streambuf);
  33.     cout.rdbuf(cout_streambuf);
  34.     cerr.rdbuf(cerr_streambuf);
  35.  
  36.     return 0;
  37. }
  38.  
  39. void printcontent(string content)
  40. {
  41.         cout << "Content-type: text/html\r\n"
  42.              << "\r\n"
  43.              << "<html>\n"
  44.              << "  <head>\n"
  45.              << "    <title>FCGI Test App</title>\n"
  46.              << "  </head>\n"
  47.              << "  <body>\n"
  48.              << "    <h1>Hello " << content << " from " << uri << " !</h1>\n"
  49.          << "  </body>\n"
  50.              << "</html>\n";
  51. }
  52.  
  53. void makejson(string content)
  54. {
  55. // Create JSON file from 'content' and save file in a predefined location
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement