X-Ray
X-Ray

Reputation: 2846

Reading string from Delphi InternetReadFile

I'm sending a JSON query to my PHP page.

Since I have been having trouble with the JSON part on the server (I'm new to JSON).

I decided to simply have the server send it back so I could see it.

Here is the JSON text I sent to it and therefore expected it to send back to me (yes, it contains dummy data):

{"propertyType":"Detached","price":"edtPrice","bedrooms":"2","bathrooms":"2","squareFeet":"1200","address":"edtAddr","city":"edtCity","postal":"edtPostal","agentPhone":"edtAgent","agentEMail":"edtEMail","agentName":"edtAgent","agentCompany":"edtCompany","Pictures":[]}

I have confirmed this is what was sent to the server and it has no #0's in the uploaded data.

Here is the code that receives it and sends it back to the client:

<?

if (file_get_contents('php://input')) {
    $json = file_get_contents('php://input');
    echo $json;

$parsedJSON = json_decode($json);
// (error here)  echo $parsedJSON->city;
}

?>

Here's the Delphi (XE3) code that receives the reply from the POST to the server:

      bRet:= HttpSendRequest(hRequest,
                             nil,
                             0,
                             PChar(sData),
                             length(sData));
      // choosing Unicode here as the format
      ss:=TStringStream.Create('', TEncoding.Unicode);
      try
        repeat
          if not InternetReadFile(hRequest, @caBuffer, SizeOf(caBuffer), iBytesRead) then
            RaiseLastOSError;

          ss.WriteBuffer(caBuffer, iBytesRead);
        until iBytesRead = 0;

        Result:=ss.DataString;
      finally
        ss.Free;
      end;

upon receiving it back from the server, it looks like the encoding is ambiguous. i wouldn't be surprised to see unicode encoding coming back from the server but i don't know what to do with this text...

caBuffer contains:

(10, 123, 0, 34, 0, 112, 0, 114, 0, 105, 0, 99, 0, 101, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 80, 0, 114, 0, 105, 0, 99, 0, 101, 0, 34, 0, 44, 0, 34, 0, 98, 0, 101, 0, 100, 0, 114, 0, 111, 0, 111, 0, 109, 0, 115, 0, 34, 0, 58, 0, 34, 0, 50, 0, 34, 0, 44, 0, 34, 0, 98, 0, 97, 0, 116, 0, 104, 0, 114, 0, 111, 0, 111, 0, 109, 0, 115, 0, 34, 0, 58, 0, 34, 0, 50, 0, 34, 0, 44, 0, 34, 0, 115, 0, 113, 0, 117, 0, 97, 0, 114, 0, 101, 0, 70, 0, 101, 0, 101, 0, 116, 0, 34, 0, 58, 0, 34, 0, 49, 0, 50, 0, 48, 0, 48, 0, 34, 0, 44, 0, 34, 0, 97, 0, 100, 0, 100, 0, 114, 0, 101, 0, 115, 0, 115, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 65, 0, 100, 0, 100, 0, 114, 0, 34, 0, 44, 0, 34, 0, 99, 0, 105, 0, 116, 0, 121, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 67, 0, 105, 0, 116, 0, 121, 0, 34, 0, 44, 0, 34, 0, 112, 0, 111, 0, 115, 0, 116, 0, 97, 0, 108, 0, 34, 0, 58, 0, 34, 0, 101, 0, 100, 0, 116, 0, 60, 98, 62, 69, 114, 114, 111, 114, 58, 60, 47, 98, 62, 32, 91, 56, 93, 32, 84, 114, 121, 105, 110, 103, 32, 116, 111, 32, 103, 101, 116, 32, 112, 114, 111, 112, 101, 114, 116, 121, 32, 111, 102, 32, 110, 111, 110, 45, 111, 98, 106, 101, 99, 116, 60, 98, 114, 62, 51, 60, 98, 114, 62, 51, 13, 10, 11, 0, 0, 0, 84, 241, 24, 0, 1, 0, 0, 0, 208, 153, 241, 113, 0, 0, 0, 0, 100, 241, 24, 0, 76, 18, 7, 0, 90, 253, 160, 93, 4, 241, 24, 0, 250, 98, 65, 117, 76, 18, 7, 0, 13, 0, 0, 0, 11, 0, 0, 0, 44, 144, 47, 1, 208, 153, 241, 113, 205, 171, 186, 220, 0, 0, 0, 0, 0, 0, 0, 0, 208, 153, 241, 113, 20, 241, 24, 0, 48, 241, 24, 0, 48, 2, 72, 117, 130, 13, 184, 93, 124, 241, 24, 0, 145, 109, 65, 117, 104, 241, 24, 0, 34, 34, 83, 0, 139, 1, 0, 0, 200, 241, 24, 0, 156, 243, 24, 0, 36, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 233, 108, 65, 117, 145, 109, 65, 117, 233, 130, 64, 0, 136, 156, 46, 1, 173, 126, 64, 0, 1, 241, 24, 0, 144, 206, 37, 1, 117, 125, 64, 0, 224, 13, 47, 1, 47, 128, 64, 0, 183, 14, 37, 0, 224, 13, 47, 1, 128, 128, 64, 0, 144, 206, 37, 1, 48, 210, 80, 0, 56, 210, 80, 0, 144, 206, 37, 1, 160, 156, 46, 1, 99, 24, 83, 0, 192, 241, 24, 0, 122, 24, 83, 0, 130, 24, 83, 0, 108, 242, 24, 0, 140, 24, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 14, 37, 0, 0, 0, 0, 0, 144, 206, 37, 1, 216, 241, 24, 0, 58, 93, 76, 0, 139, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 242, 24, 0, 250, 98, 65, 117, 172, 17, 79, 0, 139, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 14, 37, 0, 205, 171, 186, 220, 0, 0, 0, 0, 0, 0, 0, 0, 183, 14, 37, 0, 20, 242, 24, 0, 48, 242, 24, 0, 48, 2, 72, 117, 144, 206, 37, 1, 124, 242, 24, 0, 145, 109, 65, 117, 81, 109, 65, 117, 191, 255, 160, 93, 0, 0, 0, 0, 172, 17, 79, 0, 152, 31, 51, 1, 0, 0, 12, 0, 0, 184, 253, 126, 88, 242, 24, 0, 213, 98, 66, 117, 64, 170, 158, 1, 0, 0, 0, 0, 196, 242, 24, 0, 0, 184, 253, 126, 152, 31, 51, 1, 172, 17, 79, 0, 132, 242, 24, 0, 153, 203, 55, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 196, 242, 24, 0, 0, 0, 0, 0, 0, 184, 253, 126, 152, 31, 51, 1, 196, 242, 24, 0, 232, 109, 65, 117, 242, 0, 0, 0, 1, 0, 0, 0, 87, 0, 0, 0, 1, 0, 0, 0, 81, 0, 0, 0, 1, 0, 0, 0, 164, 174, 47, 1, 213, 62, 66, 0, 164, 174, 47, 1, 1, 0, 0, 0, 8, 0, 0, 0, 16, 0, 0, 0, 213, 100, 64, 0, 204, 242, 24, 0, 37, 177, 64, 0, 136, 174, 46, 1, 24, 175, 46, 1, 16, 0, 0, 0, 64, 10, 48, 1, 32, 175, 46, 1, 130, 100, 64, 0, 26, 179, 64, 0, 28, 175, 46, 1, 122, 167, 64, 0, 1, 0, 0, 0, 118, 247, 65, 0, 24, 175, 46, 1, 28, 175, 46, 1, 89, 166, 64, 0, 136, 174, 46, 1, 152, 22, 64, 0, 189, 125, 64, 0, 116, 117, 64, 0, 24, 175, 46, 1, 130, 100, 64, 0, 249, 115, 64, 0, 255, 255, 255, 255, 61, 123, 64, 0, 234, 116, 64, 0, 247, 116, 64, 0, 3, 0, 0, 0, 0, 0, 0, 0, 130, 100, 64, 0, 192, 149, 64, 0, 151, 160, 64, 0, 76, 10, 48, 1, 240, 174, 46, 1, 130, 100, 64, 0, 26, 179, 64, 0, 236, 174, 46, 1, 122, 167, 64, 0, 1, 0, 0, 0, 118, 247, 65, 0, 232, 174, 46, 1, 236, 174, 46, 1, 89, 166, 64, 0, 144, 166, 46, 1, 152, 22, 64, 0, 189, 125, 64, 0, 116, 117, 64, 0, 232, 174, 46, 1, 130, 100, 64, 0, 249, 115, 64, 0, 0, 0, 0, 0)

If I handle it like Unicode, I get this.

笊∀瀀爀椀挀攀∀㨀∀攀搀琀倀爀椀挀攀∀Ⰰ∀戀攀搀爀漀漀洀猀∀㨀∀㈀∀Ⰰ∀戀愀琀栀爀漀漀洀猀∀㨀∀㈀∀Ⰰ∀猀焀甀愀爀攀䘀攀攀琀∀㨀∀'#$3100'㈀  ∀Ⰰ∀愀搀搀爀攀猀猀∀㨀∀攀搀琀䄀搀搀爀∀Ⰰ∀挀椀琀礀∀㨀∀攀搀琀䌀椀琀礀∀Ⰰ∀瀀漀猀琀愀氀∀㨀∀攀搀琀㰀㹢牅潲㩲⼼㹢嬠崸吠祲湩'#$2067'潴朠瑥瀠潲数瑲⁹景渠湯漭橢捥㱴牢㌾

What do you think I'm doing wrong?

Thank you!

Upvotes: 1

Views: 655

Answers (1)

transilvlad
transilvlad

Reputation: 14532

php://input will provide you with the entire packet of data transmitted to the server not just the contents. This will include the headers as well.

I am not a Delphi expert but my guess is you are not sending the JSON as you think.

As far as I know this should not happen, but I am mentioning it anyway: The data you get might be archived if compression was used, but as I mentioned this should not be the case.

Unless you get a nice headers + content data it means the wrong data was sent.

Upvotes: 2

Related Questions