Home » Community » Coffee corner » Multi-UI REST applications with U++
Re: Multi-UI REST applications with U++ [message #45998 is a reply to message #45996] |
Fri, 12 February 2016 12:48  |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
NilaT wrote on Fri, 12 February 2016 11:07Hello and sorry for picking up this old Thread.
I just wanted to ask if there is a REST implementation in UPP already?
Because I searched here and this thread was the only "useful" info I found.
I need to program a webserver which uses REST communication and JSON.
For JSON I already found the Json class but for REST I'm not sure.
So, any information about this is very appreciated.
Thanks in advance.
PS: Or maybe lectus, have you done an implementation by yourself and want to share it?
There is nothing magical about REST. It is just regular HTTP server, easy to do without any special support. Skeleton code can look e.g. like this:
void RESTProcess(TcpSocket& r)
{
HttpHeader hdr;
if(hdr.Read(r)) {
VectorMap<String, String> urlvar;
String uri = hdr.GetURI();
int q = uri.Find('?');
if(q >= 0) {
ParseUrlVars(urlvar, ~uri + q + 1);
uri.Trim(q);
}
String req, n;
q = uri.Find('/');
if(q >= 0) {
n = uri.Mid(q + 1);
req = Filter(n, CharFilterAlphaToLower);
}
if(hdr.GetMethod() == "GET") {
if(req == "order") {
}
}
if(hdr.GetMethod() == "POST") {
if(req == "order") {
}
}
if(hdr.GetMethod() == "PUT") {
if(req == "order") {
}
}
if(hdr.GetMethod() == "DELETE") {
if(req == "order") {
}
}
}
HttpResponse(r, false, 400, "INVALID REQUEST");
}
TcpSocket server;
void Server()
{
for(;;) {
TcpSocket socket;
LOG("Waiting...");
bool b = socket.Accept(server);
if(b) RESTProcess(socket);
}
}
CONSOLE_APP_MAIN
{
if(!server.Listen(4000, 10)) {
LOG("Cannot open server port for listening\r\n");
return;
}
}
|
|
|
Goto Forum:
Current Time: Tue Apr 29 00:37:08 CEST 2025
Total time taken to generate the page: 0.00644 seconds
|