cgi post example requested

Just starting out? Need help? Post your questions and find answers here.
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

cgi post example requested

Post by griz »

Hi guys,

I would like to write a CGI utility that can pull POST values from an html form. I have done this with GET and have searched through the forums for some simple examples without luck. Can any of you who are experienced with PB-CGI-POST provide a simple example? How can I retrieve the "userid" and "password" values below?

Code: Select all

<html><body><center>

<FORM METHOD="POST" ACTION="checker.exe" ENCTYPE="application/x-www-form-urlencoded">
User ID : 
<INPUT TYPE="text" NAME="userid" VALUE="" SIZE="20" MAXLENGTH="100"> 
<p>
Password : 
<INPUT TYPE="password" NAME="password" VALUE="" SIZE="20" MAXLENGTH="100"> 
<p>
<INPUT TYPE="submit" NAME="submit" VALUE="Submit">
</form>

</center></body></html>
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Simple example, tested with Apache 2.0.50 (compile in console mode to your cgi-bin dir):

Code: Select all

hInput = GetStdHandle_(#STD_INPUT_HANDLE)
SetConsoleMode_(hInput, #ENABLE_LINE_INPUT|#ENABLE_ECHO_INPUT|#ENABLE_PROCESSED_INPUT)
BufferStep = 1024
BufferSize = BufferStep
*Buffer = AllocateMemory(BufferSize)
*BufferOffset = *Buffer
While ReadFile_(hInput, *BufferOffset, BufferStep-1, @bRead, 0)
  BufferSize+bRead
  *Buffer = ReAllocateMemory(*Buffer, BufferSize)
  *BufferOffset = *Buffer+BufferSize-BufferStep
Wend
Input$ = PeekS(*Buffer)
FreeMemory(*Buffer)
CloseHandle_(hInput)
hOutput = GetStdHandle_(#STD_OUTPUT_HANDLE)
HttpAnswer$ = "Content-type: text/html"+Chr(10)+Chr(13)+Chr(10)+Chr(13)+"<html><header><title>Answer from CGI</title></header><body>Hi, received your data:<br>"
HttpAnswer$+StringField(Input$, 1, "&")+"<br>"+StringField(Input$, 2, "&")
HttpAnswer$+"</body></html>"
WriteFile_(hOutput, @HttpAnswer$, Len(HttpAnswer$), @Written, 0)
CloseHandle_(hOutput)
Last edited by El_Choni on Thu Jul 08, 2004 6:05 pm, edited 1 time in total.
El_Choni
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Nice code El_Choni :)

Just a quick note, StringField index starts at 1 so you need to adjust this line to...

Code: Select all

HttpAnswer$+StringField(Input$, 1, "&")+"<br>"+StringField(Input$, 2, "&")
Image Image
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Better now, thx Paul:

Code: Select all

hInput = GetStdHandle_(#STD_INPUT_HANDLE)
SetConsoleMode_(hInput, #ENABLE_LINE_INPUT|#ENABLE_ECHO_INPUT|#ENABLE_PROCESSED_INPUT)
*ContentLength = AllocateMemory(128)
ContentLength = Val(PeekS(*ContentLength, GetEnvironmentVariable_("CONTENT_LENGTH", *ContentLength, 128)))
FreeMemory(*ContentLength)
*Buffer = AllocateMemory(ContentLength)
ReadFile_(hInput, *Buffer, ContentLength, @bRead, 0)
Input$ = PeekS(*Buffer)
FreeMemory(*Buffer)
CloseHandle_(hInput)
hOutput = GetStdHandle_(#STD_OUTPUT_HANDLE)
HttpAnswer$ = "Content-type: text/html"+Chr(10)+Chr(13)+Chr(10)+Chr(13)+"<html><header><title>Answer from CGI</title></header><body>Hi, received your data:<br>"
HttpAnswer$+StringField(StringField(Input$, 1, "&"), 1, "=")+": "+StringField(StringField(Input$, 1, "&"), 2, "=")+"<br>"+StringField(StringField(Input$, 2, "&"), 1, "=")+": "+StringField(StringField(Input$, 2, "&"), 2, "=")
HttpAnswer$+"</body></html>"
WriteFile_(hOutput, @HttpAnswer$, Len(HttpAnswer$), @Written, 0)
CloseHandle_(hOutput) 
El_Choni
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post by griz »

Thank you very much El_Choni!

This is exactly what I was looking for. :P
bluejoke
User
User
Posts: 11
Joined: Thu Nov 11, 2004 4:30 pm
Location: Guenzburg, Bavaria, Germany
Contact:

Post by bluejoke »

Muchas gracias El_Choni! Thanks for the example, I would never get it right, to pass data from my cgi-app to apache, windows-version, without getting this ***** 500 error.

Hasta luego,
Simon Ruf
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Thanks guys.. this will be VERY hand for my server..

- np
Post Reply