?
Solved

accepting post data in web executable

Posted on 2003-02-26
4
Medium Priority
?
217 Views
Last Modified: 2010-04-01
when building a web executable, how do you accept input? i understand it must be sent back to the web executable thru a form (POST), and i know how to cout >> to the browser and all ... but how do you tell the exe that this data here thats being slung at it, is the POST data?

is it the argc argv, command line arguments that will represent the POST data-incoming?

is the question clear or should i put down the b0ng and re-write my ?? ....

thanks-
f_dude
0
Comment
Question by:funkydude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:imadjinn
ID: 8027575
The data into CGI actually gets sent through stdin (for post data), so you could read it from stdin using a file pointer or an istream (cin).  You could also wrap a socket around it and get it through recv, which I think is cleaner but actually takes a little more learning.  If you use argc and argv directly, you have a problem determining where the end of the post data is (since spaces can be intermixed with the rest of the data; and you wouldn't know how many spaces or if they're tabs or whatever).  Use cin.read().  Good luck.
0
 

Author Comment

by:funkydude
ID: 8027843
hi

thanks for the great reply ... ill throw an extra 50pts if you can point me to some sample code i can learn from, as a model/example.

thank you again-
f_dude
0
 
LVL 1

Accepted Solution

by:
imadjinn earned 400 total points
ID: 8028115
Try this... it should get you on the right track.

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

string GetParameters()
{
  char *request_method = getenv("REQUEST_METHOD");
  if(request_method == NULL)
    return "";

  if(! strcmp(request_method, "GET"))
  {
    char *env = getenv("QUERY_STRING");
    if(env != NULL)
      return env;
    else
      return "";
  }
  else if(! strcmp(request_method, "POST"))
  {
    string parameters;
    int length = atoi(getenv("CONTENT_LENGTH"));
    for(int i = 0; i < length; ++i)
    {
      parameters += getc(stdin);
    }

    return parameters;
  }
  else
    return "";
}

int main(int argc, char **argv, char *envp[])
{
  cout << "Content-type: text/html\n\n";
  cout << "<html><body>";

  for(int i = 0; envp[i] != NULL; ++i)
  {
    cout << "<br/>" << envp[i] << endl;
  }

  cout << "<br/>QUERY = " << GetParameters() << endl;
  cout << "</body></html>\n";

  return 0;
}
0
 

Author Comment

by:funkydude
ID: 8028154
its a great thing when someone helps you learn a new 'thing... greatly appreciated!

f_dude
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question