Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 14 total
Thread Trying to connect to REST server
Thu, Sep 2 2021 6:45 AMPermanent Link

Michael Saunders

My first attempt at trying to  post  JSON to a REST server   
Is this issue down to me ?   I get the following error   
TIA

Application Error

Unhandled exception:

Error executing request "https://api.palletways.com/createconsignment" (No content length specified in response headers)

Trace:

Exception instance raised in unit WebHTTP at 690,10
TServerRequest.Execute in unit uMain at 416,13
TMainReqHandler.PalletwaysCon in unit uMain at 208,29
In unit WebRequest at 223,7
TRequestHandler.DoHandleRequest in unit WebRequest at 228,4
TRequestHandler.HandleRequest in unit WebRequest at 303,27
TApplication.DoRun in unit WebRequest at 331,4
TApplication.Run in application TestBed at 9,16
In application TestBed at 7,1

Here is my code :

   TempRequest:=TServerRequest.Create(nil);
  try
     with TempRequest do
        begin
           URL:= PALLETWAYS_API_PATH;
           Params.Values['apikey']:= PALLETWAYS_API_KEY;
           Params.Values['inputformat']:='json';
           Params.Values['outputformat']:='json';
           Params.Values['data ']:= jsoncontent;
           Params.Values['commit']:='false';
           Execute;
           case TempRequest.StatusCode of
              HTTP_FORBIDDEN:
                 result := temprequest.responsecontent[0];
              HTTP_OK:
                 result := temprequest.responsecontent[0];
              else
                result := temprequest.responsecontent[0];
            end;
         end;
  finally
     TempRequest.Free;
  end;
Thu, Sep 2 2021 7:18 AMPermanent Link

Walter Matte

Tactical Business Corporation

I think you need to check the result of the Execute in the event OnComplete.
When you call Execute - it does not wait for the response - you need to use the event.  

I suggest to test you drop a TServerRequest on a form - and set with a button.
If you are going to Create in code you need to Free somewhere else and the TServerRequest will need to be within the cope of the unit.

Walter



Michael Saunders wrote:

My first attempt at trying to  post  JSON to a REST server   
Is this issue down to me ?   I get the following error   
TIA

Application Error

Unhandled exception:

Error executing request "https://api.palletways.com/createconsignment" (No content length specified in response headers)

Trace:

Exception instance raised in unit WebHTTP at 690,10
TServerRequest.Execute in unit uMain at 416,13
TMainReqHandler.PalletwaysCon in unit uMain at 208,29
In unit WebRequest at 223,7
TRequestHandler.DoHandleRequest in unit WebRequest at 228,4
TRequestHandler.HandleRequest in unit WebRequest at 303,27
TApplication.DoRun in unit WebRequest at 331,4
TApplication.Run in application TestBed at 9,16
In application TestBed at 7,1

Here is my code :

   TempRequest:=TServerRequest.Create(nil);
  try
     with TempRequest do
        begin
           URL:= PALLETWAYS_API_PATH;
           Params.Values['apikey']:= PALLETWAYS_API_KEY;
           Params.Values['inputformat']:='json';
           Params.Values['outputformat']:='json';
           Params.Values['data ']:= jsoncontent;
           Params.Values['commit']:='false';
           Execute;
           case TempRequest.StatusCode of
              HTTP_FORBIDDEN:
                 result := temprequest.responsecontent[0];
              HTTP_OK:
                 result := temprequest.responsecontent[0];
              else
                result := temprequest.responsecontent[0];
            end;
         end;
  finally
     TempRequest.Free;
  end;
Thu, Sep 2 2021 9:08 AMPermanent Link

Michael Saunders

I am executing this code from a server app and within the IDE  If I  understand correctly this code runs synchronously so please confirm that I still need to use this Oncomplete event  Also if I understand correctly I can only access REST servers from my server app and not my client app

MIke


Walter Matte wrote:

I think you need to check the result of the Execute in the event OnComplete.
When you call Execute - it does not wait for the response - you need to use the event.  

I suggest to test you drop a TServerRequest on a form - and set with a button.
If you are going to Create in code you need to Free somewhere else and the TServerRequest will need to be within the cope of the unit.

Walter



Michael Saunders wrote:

My first attempt at trying to  post  JSON to a REST server   
Is this issue down to me ?   I get the following error   
TIA

Application Error

Unhandled exception:

Error executing request "https://api.palletways.com/createconsignment" (No content length specified in response headers)

Trace:

Exception instance raised in unit WebHTTP at 690,10
TServerRequest.Execute in unit uMain at 416,13
TMainReqHandler.PalletwaysCon in unit uMain at 208,29
In unit WebRequest at 223,7
TRequestHandler.DoHandleRequest in unit WebRequest at 228,4
TRequestHandler.HandleRequest in unit WebRequest at 303,27
TApplication.DoRun in unit WebRequest at 331,4
TApplication.Run in application TestBed at 9,16
In application TestBed at 7,1

Here is my code :

   TempRequest:=TServerRequest.Create(nil);
  try
     with TempRequest do
        begin
           URL:= PALLETWAYS_API_PATH;
           Params.Values['apikey']:= PALLETWAYS_API_KEY;
           Params.Values['inputformat']:='json';
           Params.Values['outputformat']:='json';
           Params.Values['data ']:= jsoncontent;
           Params.Values['commit']:='false';
           Execute;
           case TempRequest.StatusCode of
              HTTP_FORBIDDEN:
                 result := temprequest.responsecontent[0];
              HTTP_OK:
                 result := temprequest.responsecontent[0];
              else
                result := temprequest.responsecontent[0];
            end;
         end;
  finally
     TempRequest.Free;
  end;
Thu, Sep 2 2021 1:28 PMPermanent Link

Raul

Team Elevate Team Elevate

On 9/2/2021 6:45 AM, Michael Saunders wrote:
> My first attempt at trying to  post  JSON to a REST server
> Is this issue down to me ?   I get the following error
> TIA

Yes - http requests are async but you're writing it as synchronous and
freeing the http request right away.

You have few options here - easiest is to manage the http request
lifetime outside of the function itself (like at unit level) or use
TServerRequestQueue which also helps to order requests (if that is
important).

And another issue is handling the actual completion event to pass
results back - you can either do it in code if overall logic is simple
or one can use data object for example to store a callback one can call
from complete handler.

Something like this should work to make sure you get a response and then
you can work from there


procedure TForm1.Form1Create(Sender: TObject);
begin
  TempRequest := TServerRequest.Create(nil);
  TempRequest.OnComplete := WebRequestComplete;
end;

function TForm1.RunWebRequest(const jsoncontent:string):string;
begin
   try
     TempRequest.Reset;
     TempRequest.OnComplete := WebRequestComplete;
     TempRequest.URL:= PALLETWAYS_API_PATH;
     TempRequest.Params.Add('apikey=' + PALLETWAYS_API_KEY);
     TempRequest.Params.Add('inputformat=json');
     TempRequest.Params.Add('outputformat=json');
     TempRequest.Params.Add('data=' + jsoncontent);
     TempRequest.Params.Add('commit=false');
     TempRequest.Execute;
   except
    on E:Exception do
    begin
      ShowMessage('Unable to execute ' + E.Message);
    end;
  end;
end;

procedure TForm1.WebRequestComplete(Request: TServerRequest);
begin
  ShowMessage('WebRequestComplete');
  case Request.StatusCode of
    HTTP_FORBIDDEN:
      ShowMessage('Request 403 Error. Data = ' +
Request.ResponseContent.Text);
    HTTP_OK:
      ShowMessage('Request OK. Data = ' + Request.ResponseContent.Text);
    else
      ShowMessage('Other result. StatusCode = ' +
IntToStr(Request.StatusCode) + '. Response  Data = ' +
Request.ResponseContent.Text);
  end;
end;

Raul
Thu, Sep 2 2021 1:30 PMPermanent Link

Raul

Team Elevate Team Elevate

On 9/2/2021 9:08 AM, Michael Saunders wrote:
> I am executing this code from a server app and within the IDE  If I  understand correctly this code runs synchronously so please confirm that I still need to use this Oncomplete event  Also if I understand correctly I can only access REST servers from my server app and not my client app
>

No - web requests always runs async.

You can most definitely access rest servers from client app - web
request is a web request.

However if you do run it from client app you are exposing all the
details (like API key) to the client side which is easy to view.

Raul
Fri, Sep 3 2021 6:59 AMPermanent Link

Michael Saunders

Thanks for your help   I can now communicate succesfully

Mike
Tue, Sep 7 2021 6:55 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Raul,

<< No - web requests always runs async. >>

Actually, Michael is correct - the server-side TServerRequest class instances are *synchronous*-only:

https://www.elevatesoft.com/manual?action=viewcomp&id=ewb3&comp=TServerRequest

"Web server requests are executed asynchronously in client browser applications and synchronously in server applications."

It was done this way to make the server applications easier to code.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Sep 7 2021 6:58 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael

<< Is this issue down to me ?   I get the following error  >>

Who coded the API endpoint that you're calling ?  The error is exactly as specified - the EWB http request client that runs as part of the TServerRequest component is reporting that it is not receiving a Content-Length header in the response.

If the error message is incorrect, please let me know and I'll investigate further.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Sep 7 2021 3:36 PMPermanent Link

Michael Saunders

Who coded the API endpoint that you're calling ?  The error is exactly as specified - the EWB http request client that runs as part of the TServerRequest component is reporting that it is not receiving a Content-Length header in the response.

If the error message is incorrect, please let me know and I'll investigate further.


Here is a link from the REST server providers

https://api.palletways.com/documentation/en/index-en.html

Please note that following Raul's suggestion I was able to connect properly from my server app but unable to do so directly from my client app Not sure why this is I get  Status Code 0 returned

I am in email contact with Palletways support so if you have a question for them let me know  

Mike  
Thu, Sep 9 2021 7:34 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< Here is a link from the REST server providers

https://api.palletways.com/documentation/en/index-en.html >>

Have you verified that the web server on their end is actually returning a Content-Length header in the response ?

The error that you're seeing is an internal one that appears when the native HTTP request client used with server applications (it's created by the TServerRequest instance) doesn't receive a Content-Length header with a response.  I looked at this, and it may be the case that this API endpoint is not returning a response at all, in which case I may need to categorize this as a bug and make sure that the HTTP request client ignores such a situation and just doesn't try to handle the response.

<< Please note that following Raul's suggestion I was able to connect properly from my server app but unable to do so directly from my client app Not sure why this is I get  Status Code 0 returned  >>

Do you have these reversed ?  I was under the impression that the problem you were having was with the server application's TServerRequest execution, not the opposite.

Tim Young
Elevate Software
www.elevatesoft.com
Page 1 of 2Next Page »
Jump to Page:  1 2
Image