Forum > Networking and Web Programming

fphttpapp request content empty

(1/2) > >>

michoux:
Hello,

I have some problem with fphttpapp

I have follogin html code which calls my local service  coded in fphttpapp

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---<html> <head>        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script></head> <body>        <h1>Localhost printing test</h1>        <label for="url-call">URL to call:</label>        <input style="width: 400px;" type="text" id="url-call" name="url-call" value="https://127.0.0.1:8000"><br><br>        <a id="test-call" href="#">Call</a>         <p id="odgovor"></p></body> <footer>        <script type="text/javascript">                $("#test-call").click(function(){                        $.ajax({                                type: 'POST',                                url: $("#url-call").val(),                                dataType: 'json',                                data: '[{"status":"Odobren"}]',                                success: function (r) {                                        $("#odgovor").html("Odgovor: <br> Success: <br> " + JSON.stringify(r, null, 2));                                },                                error: function (r) {                                        $("#odgovor").html("Odgovor: <br> Error: <br> " + JSON.stringify(r, null, 2));                                }                        }, $(this));                });        </script></footer> </html>  Here is  just a small code to get the content of the above request.  


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure acr(req: TRequest; res: TResponse);var   rawJson: AnsiString; begin    rawJson:=req.Content;    writeln(rawjson);  end;                 
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$ curl localhost:8000/ -H 'content-type: application/json' -d '[{"x":"y"]'and it gets printed just fine.

michoux:
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:

--- Quote from: michoux on January 22, 2023, 11:02:49 am ---I am expecting JSON data but what I get is not JSON.

--- End quote ---
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program test; {$mode objfpc}{$H+} uses  sysutils, strutils; var  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';  decoded : string;  splitted : TStringArray;  part : string;begin  writeln('encoded = ', encoded);  decoded := encoded;  decoded := StringReplace(decoded, '%5B', '[', [rfReplaceAll, rfIgnorecase]);  decoded := StringReplace(decoded, '%5D', ']', [rfReplaceAll, rfIgnorecase]);   writeln('decoded = ', decoded);   writeln;  writeln('splitted:');  splitted := SplitString(decoded, '&');  for part in splitted do writeln(part);end. 
which returns:

--- Code: ---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

--- End code ---

dseligo:
OT suggestion: remove personal data from examples (there is name and some accreditationNumber).

Navigation

[0] Message Index

[#] Next page

Go to full version