Forum > Third party

HTTP/2+HTTP/1.1+WebSocket server with Free Pascal

<< < (2/5) > >>

Leledumbo:

--- Quote from: iLya2IK on May 15, 2021, 03:37:52 pm ---The structure of the project has been put in order. Added lpk file. The "permessage-deflate" extension for websocket has been fully implemented.

--- End quote ---
Perfect. Now let's see if I can make a hello world demo, surely it's much simpler than the current full fledged demo with database and all.

iLya2IK:
This is great.If you have any questions, I'm happy to answer them

nomorelogic:
really a great work!
I’m studying the source to use your wchttpserver as a RestJsonServer and I have some questions.

Question 1: QueryFields

Added in InitializeJobsTree a new endpoint


--- 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 InitializeJobsTree;begin  ...  WCJobsTree.Values['/items.json'] := TWCMyItem; end; 
Added in unit WCServerTestJobs

--- 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";}};} ---  { TWCMyItem }   TWCMyItem = class(TWCMainClientJob)  public    procedure Execute; override;  end; ... procedure TWCMyItem.Execute;var s1, s2: string;begin    s1:=Request.QueryFields.Values['id'];    s2:=Request.QueryFields.Values['name'];  // Edit: here I get an empty string    Response.Content:=TWCTestWebClient(Client).AnalizeRequest(s1, s2);     inherited Execute;end; 
and


--- 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";}};} ---function TWCTestWebClient.AnalizeRequest(const AId, AName: string): string;var jo: TJSONObject;begin  jo:=TJSONObject.Create;  try    jo.Add('Id', AId);    jo.Add('Name', AName);    result := jo.AsJSON;  finally    FreeAndNil(jo);  end;end; 
it seems that only the first of the queryfields is available: characters following "&" are ignored
using curl as test:

--- 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";}};} ---$ curl --cacert localhost.crt --request POST https://localhost:4444/items.json?id=1&name=hello{ "Id" : "1", "Name" : "" } 

Question 2: Headers
How I can read the headers of a request?


Question 3: Post data
How I can read the "post data" of a request?
(I refer to -d parameter of curl command)

--- 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";}};} ---$ curl --cacert localhost.crt --request POST https://localhost:4444/items.json?id=1&name=hello -d "{ \"ItemName\":\"AItemName\", \"ItemDescriprion\": \"AItemDescription\" }" 
Thanks you again for your work!

nomorelogic

iLya2IK:

--- Quote from: nomorelogic on May 22, 2021, 12:45:05 pm ---really a great work!

--- End quote ---

Thank you, I appreciate it.


--- Quote from: nomorelogic on May 22, 2021, 12:45:05 pm ---https://localhost:4444/items.json?id=1&name=hello -d "{ \"ItemName\":\"AItemName\", \"ItemDescriprion\": \"AItemDescription\" }"

--- End quote ---

The code you wrote is absolutely correct. I tried to write it by my own - and it works correctly from a browser - both fields are detected. Unfortunately, I was unable to reproduce the error. Please pay attention to the correctness of the command line. I don't often use curl, but maybe this post will help you https://stackoverflow.com/questions/10060093/special-characters-like-and-in-curl-post-data

For information
The project relies heavily on the class structure and fpWeb functionality. In particular, the query string analysis is performed by the fpWeb tools. The QueryFields are filled in the TRequest.ProcessQueryString (fpWeb) method, where the QueryString (this is all to the right of '?') is passed as parameter. This code is in the HTTPDefs module, but it is not part of the wcHTTPServer.

If you want to detect complex queries
All your content objects are in the Request.Content string. Example:

--- Code: Text  [+][-]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 -d '{"ItemName":"AItemName", "ItemDescriprion": "AItemDescription"}' -H "Content-Type: application/json" -X POST "http://localhost:4444/items.json?id=1&name=hello"Then parse content in Job

--- 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 TWCMyItem.Execute;var jsonObj : TJSONObject;s1, s2 : String;begin  try    s1:=Request.QueryFields.Values['id'];    s2:=Request.QueryFields.Values['name'];     jsonObj:= TJSONObject(GetJSON(Request.Content));    if assigned(jsonObj) then    begin      TWCTestWebClient(Client).DoSomething(s1, s2, jsonObj.Get('ItemName', ''),                                               jsonObj.Get('ItemDescriprion', ''));      Response.Content := cSStatusOKJSON;    end else      Response.Content := cSStatusBADJSON;  finally    if assigned(jsonObj) then FreeAndNil(jsonObj);  end;  inherited Execute;end;       
The better way for POST requests is to push all parameters to the content string (for my opinion):

--- Code: Text  [+][-]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 -d '{"id":1, "name": "hello", "ItemName":"AItemName", "ItemDescription": "AItemDescription"}' -H "Content-Type: application/json" -X POST http://localhost:4444/items.json
With included wcUtils module you can do this simple way

--- 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 TWCMyItem.Execute;var Pars : Array [0..3] of Variant;begin  // ESWGetHeaderContent replaced by DecodeJsonParams in the latest version  if ESWGetHeaderContent(Request.Content, ['id', 'name', 'ItemName', 'ItemDescription'], @(Pars[0]), [0,'','','']) then  begin    TWCTestWebClient(Client).DoSomething(Pars[0], Pars[1], Pars[2], Pars[3]);    Response.Content := cSStatusOKJSON;  end else    Response.Content := cSStatusBADJSON;  inherited Execute;end;       
After latest updates you can use the Params/ParPtr/Param properties of the TWCRequest class:

--- 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 TWCMyItem.Execute;begin  if DecodeJsonParams(Request.Content, ['id', 'name', 'ItemName', 'ItemDescription'], Params, [0,'','','']) then  begin    TWCTestWebClient(Client).DoSomething(Param[0], Param[1], Param[2], Param[3]);    Response.Content := cSStatusOKJSON;  end else    Response.Content := cSStatusBADJSON;  inherited Execute;end;       

nomorelogic:

--- Quote from: iLya2IK on May 22, 2021, 06:28:51 pm ---Please pay attention to the correctness of the command line. I don't often use curl, but maybe this post will help you https://stackoverflow.com/questions/10060093/special-characters-like-and-in-curl-post-data

--- End quote ---

Hi
as suggested in link you reported, using double quote for the entire url, it works

thanks for all the explanations
nomorelogic

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version