简体   繁体   中英

Delphi implement REST server

I have a mysql database on a server and I insert data on it using a script I have written. I have to open www.mywebsite.com/rest/create.php?param=value&param2=value2 to create a new resource, then I have to open another php file to edit an item an so on. This is the basic approach: a php file takes some parameters and then it stores files on a database.

I am using TIdHTTP because I have the possibility to do something like:

TIdHTTP1.Get(www.mywebsite.com/rest/create.php?param=value&param2=value2);

The create.php page is returning some data encoded in json. In my android app I have created a rest client that is able to parse the json received in response to the php page. For example, if I called .../show.php?id=3 the result of the page is a json with my data; later the android app will parse them and show some results to the user.


Question

Instead of calling www.mywebsite.com/rest/create.php?param=value&param2=value2 I would like to be able to do something like this www.mywebsite.com/rest/create/?param=value&param2=value2 . Basically I dont want to use the filename in *.php but I want to use the 2nd kind of URI without the filename.

The problem is that it's my first time and I don't really know what to do.

Possible solution . After reading Expert Delphi I have found a possible solution. I could create a WebBroker server for linux (so that I can export the Apache module *.so), implement some actions like this:

在此输入图像描述

When there is a request to /rest/create/ (using the OnAction event) I can run a TIdHTTP1.Get(www.mywebsite.com/rest/create.php?param=value&param2=value2); and store the result. In this way I can open the www.mywebsite.com/rest/create/ as I need and save the data. Can indy execute a get request even if it is on a linux machine (as apache module)? I have seen that the standalone server runs under indy so I guess that indy should be able to work on an apache module.

Unfortunately I am pretty new with this kind of things, actually I also don't really know what to search online to find a solution. My solution could work but I am pretty sure that there is a proper end efficient way to do this.

Note : this is just an idea after some researches I have made and I am not sure this is the proper way to do what I need. Instead of doing what I've written above, do I simply have to do something with PHP or Apache? Is there another standard (and best) way to create a REST service?

Your problem is not on Delphi side but on server one.

If you want a URL /rest/create/, you simply have to create a folder "create" under "rest" folder on your server and put an index.php file on it.

Or you can use URL rewriting rules in a .htaccess file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM