Delphi xe8 Multi-Device Json post

1

Delphi Client:

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 did not succeed, would anyone know how to make it work?

    
asked by anonymous 02.02.2016 / 17:30

1 answer

1

Well, none of you have given importance / attention to my question so I researched twice 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 of receipts we can receive the value this way:

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

I put it there to make it easy for anyone with the same problem or difficulty. Remember that if you want to send by json dai just modify and put to send by Json, But this is already the base. I hope you have helped.

    
03.02.2016 / 13:54