Delphi TIdhttp发布JSON? [英] Delphi TIdhttp Post JSON?
问题描述
PHP总是在 $ _ POST
中返回NULL,我做错了什么?
Delphi来源:
http: TIdHttp.Create(nil);
http.HandleRedirects:= True;
http.ReadTimeout = 5000;
http.Request.ContentType:='application / json';
jsonToSend:= TStringStream.Create('{name:Peter Pan}');
jsonToSend.Position:= 0;
Memo1.Lines.Text:= http.Post('http://www.website.com/test.php',jsonToSend);
jsonToSend.free;
http.free;
PHP源:
<?php
$ value = json_decode($ _ POST);
var_dump($ value);
?>
您不能使用 TStringList
发布JSON数据。 TIdHTTP.Post()
将以破坏JSON数据的方式编码 TStringList
内容。您需要将JSON数据放入 TStream
中。 TIdHTTP.Post()
将按原样传输其内容。另外,不要忘记设置 TIdHTTP.Request.ContentType
属性,以便服务器知道您要发布JSON数据。
Anybody getting JSON to work with TIdHttp ?
The PHP always return NULL in the $_POST
, am I doing anything wrong ?
Delphi source:
http := TIdHttp.Create(nil);
http.HandleRedirects := True;
http.ReadTimeout := 5000;
http.Request.ContentType := 'application/json';
jsonToSend := TStringStream.Create('{"name":"Peter Pan"}');
jsonToSend.Position := 0;
Memo1.Lines.Text := http.Post('http://www.website.com/test.php', jsonToSend);
jsonToSend.free;
http.free;
PHP source:
<?php
$value = json_decode($_POST);
var_dump($value);
?>
You can't use a TStringList
to post JSON data. TIdHTTP.Post()
will encode the TStringList
contents in a way that breaks the JSON data. You need to put the JSON data into a TStream
instead. TIdHTTP.Post()
will transmit its contents as-is. Also, don't forget to set the TIdHTTP.Request.ContentType
property so the server knows you are posting JSON data.
这篇关于Delphi TIdhttp发布JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!