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

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 552
Joined: Wed Sep 18, 2013 11:54 am
Location: France

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

Post 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.
Last edited by Joubarbe on Sat Feb 18, 2017 9:41 am, edited 1 time in total.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

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

Post 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.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Joubarbe
Enthusiast
Enthusiast
Posts: 552
Joined: Wed Sep 18, 2013 11:54 am
Location: France

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

Post 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)
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

Hi,

you should always send your string as UTF8.

Bernd
Joubarbe
Enthusiast
Enthusiast
Posts: 552
Joined: Wed Sep 18, 2013 11:54 am
Location: France

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

Post by Joubarbe »

You mean adding "Content-Type: text/html; charset=utf-8" to the header?
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post by infratec »

Yes, and...

Code: Select all

SendNetworkString(..., ..., #PB_UTF8)
But mention your ContentLength.
Bernd
DarkDragon
Addict
Addict
Posts: 2215
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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.
bye,
Daniel
Post Reply