I want to send the code and name variables by POST to a web service in php, I have this code in Delphi 10.4:
RESTRequest1.ClearBody;
//RESTRequest1.Body.Add... aquí iría lo del pase de parámetros
RESTRequest1.Method:= rmPOST;
RESTRequest1.Execute;
The idea is to receive it in php like this:
$codigo=isset($_POST['codigo']) ? $_POST['codigo'] : NULL;
$nombre=isset($_POST['nombre']) ? $_POST['nombre'] : NULL;
and thus be able to use that data sent.
another way would be to first build a JSON to pass it, I also did that like this:
content:='{'+
' "usuario": "'+usuario+'",'+
' "pass": "'+pass+'"'+
'}';
RESTRequest1.ClearBody;
RESTRequest1.Body.Add(content, TRESTContentType.ctAPPLICATION_JSON);
RESTRequest1.Method:= rmPOST;
RESTRequest1.Execute;
but content is just a set of data, that variable does not have a name so in php I cannot find the way to obtain it and use it can be in both ways, the first seems more practical to me but either of the two works for me. I am using Delphi RAD studio community edition 10.4
There are several ways to send data (or parameters) in the request, depending on how they must reach the Webservice in php.
A simple way is to send them as parameters in the request header itself, using the component
TRESTClient
that you must have. It would be a code similar to the one below:Another option is to use the body to send the information, as you mentioned, using a JSON with the information. In that case you can use the
Body
component propertyTRESTRequest
. You can try code similar to this:Where
sContent
is theJSON
(like the one you used in the question).To retrieve this last information in php (sent in the body) you can try something like this: