Recent

Author Topic: POST/GET Problem  (Read 2049 times)

ahiggins

  • Jr. Member
  • **
  • Posts: 90
POST/GET Problem
« on: March 06, 2018, 06:09:28 pm »
Hi, I'm having issues using POST to send data from a HTML form to my Apache Module

Code: Pascal  [Select]
  1. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2.   AResponse: TResponse; Var Handled: Boolean);
  3. var
  4.    Str       : String;
  5.  
  6. begin
  7.      ARequest.ContentType:='APPLICATION/X-WWW-FORM-URLENCODED';
  8.      Str:='[returned data] - '+ARequest.QueryFields.Values['pname'];
  9.      AResponse.Content:=Str;
  10.      Handled:=True;
  11. end;
  12.  

the above works fine if i sent the the data via a  simple form using GET

Code: Text  [Select]
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <form action="http://localhost/api" method="GET">
  4.   <div>
  5.     <label for="pname">What is your name?</label>
  6.     <input name="pname" id="pname" value="Jon Doe">
  7.   </div>
  8.   <div>
  9.   </div>
  10.   <div>
  11.     <button>Send</button>
  12.   </div>
  13. </form>
  14.  

the Apache module returns the below correctly.
[returned data] - Jon Doe

but if i change the code to handle POST
Code: Pascal  [Select]
  1.  
  2. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  3.   AResponse: TResponse; Var Handled: Boolean);
  4. var
  5.    Str       : String;
  6.  
  7. begin
  8.      ARequest.ContentType:='APPLICATION/X-WWW-FORM-URLENCODED';
  9.      Str:='[returned data] - '+ARequest.Content;
  10.      AResponse.Content:=Str;
  11.      Handled:=True;
  12. end;
  13.  

and send the data with a simple html form using POST

Code: Text  [Select]
  1. <form action="http://localhost/api" method="POST">
  2.   <div>
  3.     <label for="pname">What is your name?</label>
  4.     <input name="pname" id="pname" value="Jon Doe">
  5.   </div>
  6.   <div>
  7.   </div>
  8.   <div>
  9.     <button>Send</button>
  10.   </div>
  11. </form>
  12.  

the Apache module fails to  return any data.
[returned data] -

any help would be great.

Tony

jshah

  • Full Member
  • ***
  • Posts: 128
Re: POST/GET Problem
« Reply #1 on: March 06, 2018, 09:50:57 pm »
Try

1. ARequest.ContentFields.Values['<field-name>'];

2. Maybe input tag missing type attribute

ahiggins

  • Jr. Member
  • **
  • Posts: 90
Re: POST/GET Problem
« Reply #2 on: March 07, 2018, 01:26:39 pm »
@jshah, I shall give it a try

kinds regards

Tony

ahiggins

  • Jr. Member
  • **
  • Posts: 90
Re: POST/GET Problem
« Reply #3 on: March 07, 2018, 03:07:36 pm »
ok, I tried
Code: Pascal  [Select]
  1.  ARequest.ContentFields.Values['pname'];
still no joy.

also noticed
Code: Pascal  [Select]
  1. ARequest.ContentFields.count
returns 0

any ideas are more than welcome


Tony

egsuh

  • Full Member
  • ***
  • Posts: 174
Re: POST/GET Problem
« Reply #4 on: March 07, 2018, 03:27:27 pm »
Try after changing your html.

<input type="TEXT" name=pname value="John Dow">

type=text is missing in your example.

rvk

  • Hero Member
  • *****
  • Posts: 3508
Re: POST/GET Problem
« Reply #5 on: March 07, 2018, 03:31:59 pm »
And you shouldn't SET ARequest.ContentType. It's for input of the request.
If you want you can set the AResponse.ContentType.

B.T.W. what is the value of ARequest.ContentType when entering that event?

ahiggins

  • Jr. Member
  • **
  • Posts: 90
Re: POST/GET Problem
« Reply #6 on: March 07, 2018, 03:37:32 pm »
@rvk

ARequest.ContentType is application/x-www-form-urlencoded when entering the event

I removed the line assigning ARequest.ContentType still no output
« Last Edit: March 07, 2018, 03:40:41 pm by ahiggins »

rvk

  • Hero Member
  • *****
  • Posts: 3508
Re: POST/GET Problem
« Reply #7 on: March 07, 2018, 03:47:57 pm »
I removed the line assigning ARequest.ContentType still no output
No, that wasn't the problem.
It was just not correct to set the ContentType of the incoming request.
You need to do that on the ARepsonse (outgoing) request.
But that's also not your actual problem.

Try setting type=text for the input like jshah and egsuh suggested.

I also missed the type=submit for the button. I wonder if the browser actually does a POST when there is no type=submit in the button tag. So put that in there too.

Code: Text  [Select]
  1. <form action="http://localhost/api" method="POST">
  2.   <div>
  3.     <label for="pname">What is your name?</label>
  4.     <input type=text name="pname" id="pname" value="Jon Doe">
  5.   </div>
  6.   <div>
  7.   </div>
  8.   <div>
  9.     <button type=submit>Send</button>
  10.   </div>
  11. </form>

ahiggins

  • Jr. Member
  • **
  • Posts: 90
Re: POST/GET Problem
« Reply #8 on: March 07, 2018, 04:08:54 pm »
@rvk

set the type to text and the added submit type still not returning anything when using POST



<!DOCTYPE html>
<html>
<head></head>
<body>   
<div class="content">
    <form action="http://localhost/api" method="POST">
        <label>Name: </label>
        <input name="name" type="text" id="name" size="25" />

        <input name="Submit" type="submit" value="Submit!" />
    </form>
</div>
</body>
</html>


egsuh

  • Full Member
  • ***
  • Posts: 174
Re: POST/GET Problem
« Reply #9 on: March 07, 2018, 04:20:48 pm »
I tested this HTML on my PC just changing "action" part (on Windows 8 ), and NO PROBLEM.
I did make a web-server program displaying the content of  "contentfields" in the past (fcgi module) for testing purpose. 
I do not use Apache server so the problem might lies in that part.
« Last Edit: March 07, 2018, 04:23:01 pm by egsuh »

ahiggins

  • Jr. Member
  • **
  • Posts: 90
Re: POST/GET Problem
« Reply #10 on: March 07, 2018, 04:49:55 pm »
@egsuh

Starting to think its a setup issue somewhere myself, I running Debian 9 and Apache 2.2
might setup another box from scratch and see it anything changes.

it just seems strange that it works with the GET method.
« Last Edit: March 07, 2018, 06:05:55 pm by ahiggins »