fpweb and reading the contents of a request

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

fpweb and reading the contents of a request

Felipe Monteiro de Carvalho
Hello,

I am trying to send data from javascript and read data back from my
cgi module with this:

  SendSyncRequest = function(RequestURL, RequestContent)
  {
    var request = new XMLHttpRequest();
    request.open('GET', RequestURL, false);
    request.setRequestHeader('Content-Type', 'text/html');
    request.send(RequestContent);
    //debugDiv.innerHTML = debugDiv.innerHTML + "after send<br>";
    return request.responseText;
  }

    var tableInnerHTML =
SendSyncRequest("mywebapp.cgi?module=LobbyServer", "$200");

Which works, except for 1 thing. I wanted to read this "$200" in my
web module, but:

procedure TwebLobbyServer.DataModuleRequest(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
var
  LobbyComm: TLobbyComm;
  OPResponse: TGameResponse = nil;
  Msg: TMarshallable;
begin
  WebDebugOut('[TwebLobbyServer.DataModuleRequest] ARequest.Content='
+ ARequest.Content);

With this code I receive an empty ARequest.Content

Any ideas?

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Michael Van Canneyt


On Mon, 17 Oct 2011, Felipe Monteiro de Carvalho wrote:

> Hello,
>
> I am trying to send data from javascript and read data back from my
> cgi module with this:
>
>  SendSyncRequest = function(RequestURL, RequestContent)
>  {
>    var request = new XMLHttpRequest();
>    request.open('GET', RequestURL, false);
>    request.setRequestHeader('Content-Type', 'text/html');
>    request.send(RequestContent);
>    //debugDiv.innerHTML = debugDiv.innerHTML + "after send<br>";
>    return request.responseText;

As far as I know, this will always be empty,
for the simple reason that the request is executed asynchronously.
You must attach a callback which is executed once the request is
ready, and read ResponseText after that.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Felipe Monteiro de Carvalho
On Mon, Oct 17, 2011 at 3:55 PM, Michael Van Canneyt
<[hidden email]> wrote:
> As far as I know, this will always be empty, for the simple reason that the
> request is executed asynchronously. You must attach a callback which is
> executed once the request is ready, and read ResponseText after that.

Ah, no, my problem is not in the JavaScript ... my syncronous request
works fine in Opera at least. My problem is in fpweb:

procedure TwebLobbyServer.DataModuleRequest(Sender: TObject;
  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
 begin
  WebDebugOut('[TwebLobbyServer.DataModuleRequest] ARequest.Content='
 + ARequest.Content); <--------------- HERE

This is where I get an empty ARequest.Content

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Michael Van Canneyt


On Mon, 17 Oct 2011, Felipe Monteiro de Carvalho wrote:

> On Mon, Oct 17, 2011 at 3:55 PM, Michael Van Canneyt
> <[hidden email]> wrote:
>> As far as I know, this will always be empty, for the simple reason that the
>> request is executed asynchronously. You must attach a callback which is
>> executed once the request is ready, and read ResponseText after that.
>
> Ah, no, my problem is not in the JavaScript ... my syncronous request
> works fine in Opera at least.

? AFAIK xhttprequest is never synchronous...

> My problem is in fpweb:
>
> procedure TwebLobbyServer.DataModuleRequest(Sender: TObject;
>  ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
> begin
>  WebDebugOut('[TwebLobbyServer.DataModuleRequest] ARequest.Content='
> + ARequest.Content); <--------------- HERE
>
> This is where I get an empty ARequest.Content

Did you properly encode the content ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Felipe Monteiro de Carvalho
On Mon, Oct 17, 2011 at 4:01 PM, Michael Van Canneyt
<[hidden email]> wrote:
> ? AFAIK xhttprequest is never synchronous...

It is synchronous if the third parameter of open is set to false:

http://www.w3.org/TR/XMLHttpRequest/#the-open-method

client . open(method, url, async, user, password)

The asynchronous flag
True when fetching is done asychronously. False when fetching is done
synchronously.

> Did you properly encode the content ?

What exactly do I need to do? Pass a header saying it is UTF-8?

I am just sending simple text.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Michael Van Canneyt


On Mon, 17 Oct 2011, Felipe Monteiro de Carvalho wrote:

> On Mon, Oct 17, 2011 at 4:01 PM, Michael Van Canneyt
> <[hidden email]> wrote:
>> ? AFAIK xhttprequest is never synchronous...
>
> It is synchronous if the third parameter of open is set to false:
>
> http://www.w3.org/TR/XMLHttpRequest/#the-open-method
>
> client . open(method, url, async, user, password)
>
> The asynchronous flag
> True when fetching is done asychronously. False when fetching is done
> synchronously.
>
>> Did you properly encode the content ?
>
> What exactly do I need to do? Pass a header saying it is UTF-8?
>
> I am just sending simple text.

Ehm. I think that the problem is elsewhere. Content is only accepted with the POST method.

Michael.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Felipe Monteiro de Carvalho
On Mon, Oct 17, 2011 at 4:11 PM, Michael Van Canneyt
<[hidden email]> wrote:
> Ehm. I think that the problem is elsewhere. Content is only accepted with
> the POST method.

Why? Is this a limitation from HTTP?

If I change to POST then I can no longer select my module, neither
"?module=MainPage" nor "/MainPage" worked. In fact then I get a very
strange error message:

Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.

With this Apache log:

[Mon Oct 17 16:20:15 2011] [error] [client 127.0.0.1] Premature end of
script headers: openpoker3.cgi, referer:
http://localhost:8080/cgi-bin/openpoker3.cgi?module=MainPage

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Michael Van Canneyt


On Mon, 17 Oct 2011, Felipe Monteiro de Carvalho wrote:

> On Mon, Oct 17, 2011 at 4:11 PM, Michael Van Canneyt
> <[hidden email]> wrote:
>> Ehm. I think that the problem is elsewhere. Content is only accepted with
>> the POST method.
>
> Why? Is this a limitation from HTTP?

Yes.

>
> If I change to POST then I can no longer select my module, neither
> "?module=MainPage" nor "/MainPage" worked. In fact then I get a very
> strange error message:
>
> Internal Server Error
> The server encountered an internal error or misconfiguration and was
> unable to complete your request.

You must specify an action. A single path component is treated as an action.
?module=MainPage&Action=MyAction


Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Felipe Monteiro de Carvalho
On Mon, Oct 17, 2011 at 4:33 PM, Michael Van Canneyt
<[hidden email]> wrote:
> Yes.

I see ... simply encoding the data differently from what I originally
wanted and feeding it to a GET field worked fine.

> You must specify an action. A single path component is treated as an action.
> ?module=MainPage&Action=MyAction

That didn't help. Also I was not using actions before and it worked
with GET without specifying an action.

I think that this part of the URL cannot be used in POST, so we would
need another way to select the module. Although I might be wrong.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Michael Van Canneyt


On Mon, 17 Oct 2011, Felipe Monteiro de Carvalho wrote:

> On Mon, Oct 17, 2011 at 4:33 PM, Michael Van Canneyt
> <[hidden email]> wrote:
>> Yes.
>
> I see ... simply encoding the data differently from what I originally
> wanted and feeding it to a GET field worked fine.

Also a solution :-)

>
>> You must specify an action. A single path component is treated as an action.
>> ?module=MainPage&Action=MyAction
>
> That didn't help. Also I was not using actions before and it worked
> with GET without specifying an action.

That's because the system assumes certain defaults.

>
> I think that this part of the URL cannot be used in POST, so we would
> need another way to select the module. Although I might be wrong.

The method of determining the module/action does not depend on the method.
Either PATHINFO is used, or the module and action request variables are used.
You can perfectly do a post with an URL that contains "?module=MainPage&Action=MyAction"

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

herux
Try using Indy or Synapse as HttpClient to send data content  using HTTP POST with the parameters as you mean. my FPWeb success to do it using PHP via CURL.

I guess this is not a limitation of HTTP, just not the standard for web scripting
-
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Sven Barth-2
Am 18.10.2011 11:35, schrieb herux:
> Try using Indy or Synapse as HttpClient to send data content  using HTTP POST
> with the parameters as you mean. my FPWeb success to do it using PHP via
> CURL.
>
> I guess this is not a limitation of HTTP, just not the standard for web
> scripting

One should at least consider the following statement from
http://en.wikipedia.org/wiki/HTTP#Safe_methods :

Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as
safe, which means they are intended only for information retrieval and
should not change the state of the server. In other words, they should
not have side effects, beyond relatively harmless effects such as
logging, caching, the serving of banner advertisements or incrementing a
web counter. Making arbitrary GET requests without regard to the context
of the application's state should therefore be considered safe.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpweb and reading the contents of a request

Felipe Monteiro de Carvalho
In reply to this post by herux
On Tue, Oct 18, 2011 at 11:35 AM, herux <[hidden email]> wrote:
> Try using Indy or Synapse as HttpClient to send data content  using HTTP POST
> with the parameters as you mean. my FPWeb success to do it using PHP via
> CURL.

!? My problem was sending data from JavaScript to FPWeb, I fail to see
how any of this could apply.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal