Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-10

fpweb and reading the contents of a request

13 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 13 lines
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.

Reply by email

Felipe Monteiro de Carvalho
to Michael Van Canneyt
Michael Van Canneyt 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 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

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 8 lines
On Mon, Oct 17, 2011 at 3:55 PM, Michael Van Canneyt 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...

Show quoted text 9 lines
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.

Reply by email

Felipe Monteiro de Carvalho
to Michael Van Canneyt
Michael Van Canneyt 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

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 19 lines
On Mon, Oct 17, 2011 at 4:01 PM, Michael Van Canneyt 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.

Reply by email

Felipe Monteiro de Carvalho
to Michael Van Canneyt
Michael Van Canneyt 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

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 6 lines
On Mon, Oct 17, 2011 at 4:11 PM, Michael Van Canneyt 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.

Show quoted text 8 lines
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.

Reply by email

Felipe Monteiro de Carvalho
to Michael Van Canneyt
Michael Van Canneyt 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

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 6 lines
On Mon, Oct 17, 2011 at 4:33 PM, Michael Van Canneyt 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 :-)

Show quoted text 6 lines
> 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.

Reply by email

herux
to Michael Van Canneyt

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

-----
-

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/fpweb-and-reading-the-contents-of-a-request-tp4909915p4913056.html Sent from the Free Pascal - General mailing list .

Reply by email

Sven Barth
to herux
herux wrote 6 lines
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

Reply by email