1

I have a little problem with sending JSON data to a PHP file.

I am using XE10 (update 1) with Indy 10.6.2.
iDHTTP contenttype is application/json
Contentencoding is utf-8

I have a variable JS with correct JSON data.
I have a variable AD with valid adres to the PHP file.
If I put the output of AD + JS in the browser my PHP file is doing what it should do, i.e. creating a file with the JSON values.

But, when I do idHTTP.GET(AD + JS);, the PHP file creates an empty file.
So the PHP scipt gets called but without the JSON data.
Can someone help me PLEASE, why the data is not sending.

BTW Can I use the GET method (If not then how can I do this)?

Here is the code:

Procedure Twebform.NaPost(k : TJSONObject );
var 
  str : WideString;
begin
  str := 'http://www.mywebsite.com/test.php?value='+k.ToString;
  try
    IdHTTPJ.GET(str);
  except on E:Exception do
    begin
      ShowMessage(E.Message);
    end;
  end;
end;
George
  • 41
  • 3
  • I found the issue, it is not working becouse in some of the values are spaces between words. So if i stringreplace(value, ' ','',[rlAll]) then it works?? Weard why its breaking on spaces? – George Feb 02 '16 at 15:40
  • 1
    Because an URL does not support spaces? You must encode your URI strings... – whosrdaddy Feb 02 '16 at 15:51
  • Duplicate http://stackoverflow.com/questions/35065899/idhttp-post-error-http-1-1-401 . Or also http://stackoverflow.com/questions/24025646/delphi-http-post-json – smooty86 Feb 02 '16 at 17:49
  • 1
    @George: The correct way to encode the JSON string in the URL query string is to use `TIdURI.ParamsEncode()` instead of `StringReplace()`, eg: `str := 'http://www.mywebsite.com/test.php?value='+TIdURI.ParamsEncode(k.ToString);` – Remy Lebeau Feb 02 '16 at 18:00
  • 1
    @smooty86: both of those links deal with `POST` requests. George is using a `GET` request instead (which is an odd way to send JSON, but is still a valid method if that is what the PHP is actually expecting, and the JSON does not exceed the server's max URL length limit). – Remy Lebeau Feb 02 '16 at 18:01
  • Thanks guys for replies. I am using GET because its easy for me now. But the problem was that some values in JSON string are not UTF8 compatible and that's why it was empty. – George Jan 17 '17 at 20:48

0 Answers0