Recent

Author Topic: fphttpapp request content empty  (Read 533 times)

michoux

  • Jr. Member
  • **
  • Posts: 89
fphttpapp request content empty
« on: January 21, 2023, 10:30:21 am »
Hello,

I have some problem with fphttpapp

I have follogin html code which calls my local service  coded in fphttpapp
Code: Pascal  [Select][+][-]
  1. <html>
  2.  
  3. <head>
  4.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
  5. </head>
  6.  
  7. <body>
  8.         <h1>Localhost printing test</h1>
  9.         <label for="url-call">URL to call:</label>
  10.         <input style="width: 400px;" type="text" id="url-call" name="url-call" value="https://127.0.0.1:8000"><br><br>
  11.         <a id="test-call" href="#">Call</a>
  12.  
  13.         <p id="odgovor"></p>
  14. </body>
  15.  
  16. <footer>
  17.         <script type="text/javascript">
  18.                 $("#test-call").click(function(){
  19.                         $.ajax({
  20.                                 type: 'POST',
  21.                                 url: $("#url-call").val(),
  22.                                 dataType: 'json',
  23.                                 data: '[{"status":"Odobren"}]',
  24.                                 success: function (r) {
  25.                                         $("#odgovor").html("Odgovor: <br> Success: <br> " + JSON.stringify(r, null, 2));
  26.                                 },
  27.                                 error: function (r) {
  28.                                         $("#odgovor").html("Odgovor: <br> Error: <br> " + JSON.stringify(r, null, 2));
  29.                                 }
  30.                         }, $(this));
  31.                 });
  32.         </script>
  33. </footer>
  34.  
  35. </html>
  36.  
  37.  
  38. Here is  just a small code to get the content of the above request.
  39.  
  40.  


Code: Pascal  [Select][+][-]
  1. procedure acr(req: TRequest; res: TResponse);
  2. var
  3.  
  4.   rawJson: AnsiString;
  5.  
  6. begin
  7.     rawJson:=req.Content;
  8.     writeln(rawjson);
  9.  
  10. end;              
  11.  
  12.  
  13.  

If for example the json data in html is not correctly encoded in brackets Like this data: '[{"status":"Odobren"}]', then I do not receive anything on my local service in req.Content;

Is there any way to check if there was actuall connection made on fphttpapp?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8557
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fphttpapp request content empty
« Reply #1 on: January 22, 2023, 06:49:34 am »
I can only guess, but are you sure you set up the https termination correctly? I would try http first. And after you solve that, ensure you handle CORS correctly or serve the client HTML script from the server, as the base address must match in order not to trigger CORS.

fphttpserver / fphttpapp does not do any automatic body conversion and the request content should be readable as is, provided it's actually gone through. As an example, using curl I can even hit broken JSON to your code:
Code: Bash  [Select][+][-]
  1. $ curl localhost:8000/ -H 'content-type: application/json' -d '[{"x":"y"]'
and it gets printed just fine.
« Last Edit: January 22, 2023, 06:54:26 am by Leledumbo »

michoux

  • Jr. Member
  • **
  • Posts: 89
Re: fphttpapp request content empty
« Reply #2 on: January 22, 2023, 11:02:49 am »
Hi,

Now I fixed it, I mean it was fixed from web site.

Another issue is that data which I receive is like this.

data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data%5B0%5D%5Blastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5B0%5D%5BaccreditationNumber%5D=2023001000372&data%5B0%5

I am expecting JSON data but what I get is not JSON.
I try to speak with developers from this web site and they claim that they are sending proper JSON data.
Can I do something with above data, or I need to discuss with web developers to send me correct.

TRon

  • Hero Member
  • *****
  • Posts: 834
Re: fphttpapp request content empty
« Reply #3 on: January 22, 2023, 11:09:36 am »
I am expecting JSON data but what I get is not JSON.
It /is/ JSON

You need to decode the HTML entities to plain text. see also: https://stackoverflow.com/questions/9966053/what-does-5b-and-5d-in-post-requests-stand-for

edit:
I agree that this is a very odd way of returning JSON values (e.g. not standard)

simple example:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils, strutils;
  7.  
  8. var
  9.   encoded : string = 'data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data%5B0%5D%5Blastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5B0%5D%5BaccreditationNumber%5D=2023001000372&data%5B0%5';
  10.   decoded : string;
  11.   splitted : TStringArray;
  12.   part : string;
  13. begin
  14.   writeln('encoded = ', encoded);
  15.   decoded := encoded;
  16.   decoded := StringReplace(decoded, '%5B', '[', [rfReplaceAll, rfIgnorecase]);
  17.   decoded := StringReplace(decoded, '%5D', ']', [rfReplaceAll, rfIgnorecase]);
  18.  
  19.   writeln('decoded = ', decoded);
  20.  
  21.   writeln;
  22.   writeln('splitted:');
  23.   splitted := SplitString(decoded, '&');
  24.   for part in splitted do writeln(part);
  25. end.
  26.  

which returns:
Code: [Select]
encoded = data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data%5B0%5D%5Blastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5B0%5D%5BaccreditationNumber%5D=2023001000372&data%5B0%5
decoded = data[0][vip]=0&data[0][firstname]=Mojca&data[0][lastname]=Smolej&data[0][country]=Slovenija&data[0][accreditationNumber]=2023001000372&data[0%5

splitted:
data[0][vip]=0
data[0][firstname]=Mojca
data[0][lastname]=Smolej
data[0][country]=Slovenija
data[0][accreditationNumber]=2023001000372
data[0%5
« Last Edit: January 22, 2023, 11:34:51 am by TRon »

dseligo

  • Hero Member
  • *****
  • Posts: 960
Re: fphttpapp request content empty
« Reply #4 on: January 22, 2023, 07:30:55 pm »
OT suggestion: remove personal data from examples (there is name and some accreditationNumber).

Leledumbo

  • Hero Member
  • *****
  • Posts: 8557
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fphttpapp request content empty
« Reply #5 on: January 23, 2023, 04:23:25 am »
Another issue is that data which I receive is like this.

data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data%5B0%5D%5Blastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5B0%5D%5BaccreditationNumber%5D=2023001000372&data%5B0%5

I am expecting JSON data but what I get is not JSON.
I try to speak with developers from this web site and they claim that they are sending proper JSON data.
Can I do something with above data, or I need to discuss with web developers to send me correct.
I can't tell, is this what req.Content spits? Or is it a result of your code decoding it? Certainly it is not JSON, it's a urlencoded instead:
Quote
"the keys and values are encoded in key-value tuples separated by '&', with a '=' between the key and the value"
which is exactly what your string is, if you urldecode that %XX chars:
Code: [Select]
data[0][vip]=0&data[0][firstname]=Mojca&data[0][lastname]=Smolej&data[0][country]=Slovenija&data[0][accreditationNumber]=2023001000372&data[0%5(yes, you gave truncated data)

 

TinyPortal © 2005-2018