Page 1 of 1

[SOLVED] Respond to an HTTP request (noob question!)

Posted: Fri Feb 17, 2017 4:44 pm
by Joubarbe
Hi,

I'm pretty new to web development, and I'm playing with various stuff right now, but what I'd like to do and understand is how to make a communication between a client and a PureBasic server.

The idea is to send a jQuery get request with the following code :

Code: Select all

$("#get-button").on("click", function(event) {
    $.get("http://localhost:6832", function(data) {
        console.log(data);
    });
});
I receive the connection (from the PureBasic example NetworkServer.pb), but I don't know what to answer to make it to the callback and print "data" into the console (I naively thought that SendNetworkString(ClientID) would be enough...). Besides, it would be good to send something else than a HTTP header from JS.

Re: Respond to an HTTP request (noob question!)

Posted: Sat Feb 18, 2017 3:44 am
by Lunasole
Hi. It looks like you need HTTP server, not just a TCP/UDP server (which is NetworkServer.pb).

I don't remember such HTTP server examples on PB and didn't yet make attempts to make own, but here is some example of server responce for GET query:

Code: Select all

HTTP/1.1 200 OK
Content-Type: text/html
Date: Sat, 18 Feb 2017 02:36:59 GMT
Expires: Sat, 25 Feb 2017 02:36:59 GMT
Last-Modified: Fri, 09 Aug 2033 23:54:35 GMT
Server: YourHTTPSERVER
Content-Length: 95

HERE IS STRING TO PRINT IN BROWSER CONSOLE, SET CORRECT Content-Length TO A SIZE OF THIS STRING
You should send data like this using SendNetworkString(). #CRLF sequence used as new line separator if I'm not wrong.

Re: Respond to an HTTP request (noob question!)

Posted: Sat Feb 18, 2017 9:03 am
by Joubarbe
Thanks Lunasole, your message makes me searched in the right direction and I came with that solution:

Code: Select all

Define response.s = ~"HTTP/1.1 200 OK\r\nCache-Control: no-cache, private\r\nContent-Length: 95\r\nDate: Mon, 24 Nov 2014 10:21:21 GMT\r\nAccess-Control-Allow-Origin: *\r\nConnection: close\r\n\r\n" +
                    "HERE IS STRING To PRINT IN BROWSER CONSOLE, SET CORRECT Content-Length To A SIZE OF THIS STRING"
\r\n\r\n at the end is important, as well as Access-Control-Allow-Origin. Connection: close is optional though (I assume it's better to close the connection if the client has nothing more to say...).
Date should be properly pre-formatted somewhere.

It's quite fun to manipulate low level requests ; I shoud learn node.JS :)

Now I just have to figure out how to send parameters to the server (aka understand the $.ajax() function).

EDIT: which is:

Code: Select all

$.ajax("http://localhost:6832", { data: "THIS_IS_DATA_FROM_CLIENT" }).done(function (data) {
    console.log(data);
});
And in PB, something like that (messy but it works ^^):

Code: Select all

ReceiveNetworkData(ClientID, *Buffer, 1000)
					
Define clientRequest.s = PeekS(*Buffer, -1, #PB_UTF8) ; HTTP request header
Define clientData.s = StringField(StringField(clientRequest, 2, "/?"), 1, " ")
					
MessageRequester("Client data", clientData) ; parameters from client
SendNetworkString(ClientID, response) ; parameters to client (response)

Re: [SOLVED] Respond to an HTTP request (noob question!)

Posted: Sat Feb 18, 2017 11:04 am
by infratec
Hi,

you should always send your string as UTF8.

Bernd

Re: [SOLVED] Respond to an HTTP request (noob question!)

Posted: Sat Feb 18, 2017 12:10 pm
by Joubarbe
You mean adding "Content-Type: text/html; charset=utf-8" to the header?

Re: [SOLVED] Respond to an HTTP request (noob question!)

Posted: Sat Feb 18, 2017 12:53 pm
by infratec
Yes, and...

Code: Select all

SendNetworkString(..., ..., #PB_UTF8)
But mention your ContentLength.
Bernd

Re: Respond to an HTTP request (noob question!)

Posted: Sat Feb 18, 2017 4:11 pm
by DarkDragon
Lunasole wrote:Hi. It looks like you need HTTP server, not just a TCP/UDP server (which is NetworkServer.pb).

I don't remember such HTTP server examples on PB and didn't yet make attempts to make own
There is the Atomic Webserver.