Delphi xe8 Multi-device Json post

Asked

Viewed 524 times

1

Good staff I would like to send data by json to a php server from Delphi, I tried to do this way:

Delphi Cliente:

procedure TForm7.Button7Click(Sender: TObject);
var
  code       : Integer;
  sResponse  : String;
  Json       : String;
  JsonToSend : TStringStream;
begin
  Json       := '{"email" : "[email protected]", "password" : "123testar"}';
  JsonToSend := TStringStream.Create( UTF8Encode(Json) );
  try
    IdHTTP1.Request.Method               := 'POST';
    IdHTTP1.Request.ContentType       := 'application/json';
    IdHTTP1.Request.ContentEncoding := 'utf-8';

    try
      sResponse := IdHTTP1.Post('http://localhost/webservice/receber.php', JsonToSend);
    except
      on E: Exception do
      begin
        Memo1.Lines.Add('Error on request: '#13#10 + e.Message);
        Exit;
      end;
    end;

    Memo1.lines.clear;
    Memo1.lines.add(sResponse);
  finally
    JsonToSend.Free();
  end;
end;

PHP server:

<?php
$jason_data = file_get_contents('http://localhost/webservice/receber.php');
$decoded_data = json_decode($json_data);
print_r($decoded_data);
?>

But I didn’t succeed, Someone would know how to make it work?

1 answer

1

Good as none of you gave importance/attention to my question so I researched double and managed to solve my problem. The code below shows how to send values through Indy POST to a page:

 var
Parameters : TStringList;
Begin
  Parameters := TStringList.Create;
  Parameters.Add('nome=lucas');
  Memo1.Text:= IdHTTP1.Post('http://localhost/webservice/receber.php',Parameters);
End;

And in PHP receipts we can receive the value in this way:

<?php
if(isset($_POST['nome'])){
echo $_POST['nome'];
}
?>

I posted there to make it easier for those with the same problem or difficulty. Remembering that if you want to send by json then just modify and put to send by json, but this one is already the basis. I hope it has helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.