[SOLVED] [Brook] Address book question: handling 404 lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- BigChimp · 2013-02-19 09:41 Hi all (& Silvio ) In the Brook REST addressbook client application there's code like this: Code: [Select]class function TfrCustomEdit.ProcessRequest(   const AHttpResult: THttpResult): Boolean; begin   Result := (AHttpResult.Code = 200) or (AHttpResult.Code = 201) or     (AHttpResult.Code = 204) or (AHttpResult.Code = 404);   if not Result then     ShowMessageFmt('ERROR: Text: %s; code: %d.',       [AHttpResult.Text, AHttpResult.Code]); end; It seems like 404 errors are silently ignored. Doesn't that hide a bit too much (note: REST/web programming newb here, I'm just assuming 404 means something like "resource not found") ---------------------------------------------------------------------- Silvio Clécio · 2013-02-19 22:52 Hello guy! 404 is handled by the broker, so it can be returned by the requester. ---------------------------------------------------------------------- Silvio Clécio · 2013-02-19 22:54 ... the result for 404 is: Code: [Select]{ "error": "Not found." } ---------------------------------------------------------------------- BigChimp · 2013-02-20 06:50 Thanks Silvio... I'm not clear about how the addressbook client goes through Brook then but I'll figure it out ---------------------------------------------------------------------- BigChimp · 2013-02-21 09:22 Had another look and I'm fairly sure you are talking about the *server* part, not the addressbook *client* part. ---------------------------------------------------------------------- Silvio Clécio · 2013-02-21 14:46 Hello, I use two tools to debug my REST APIs: . http://imagebin.org/247563 (HttpRequester - Firefox plugin) . http://imagebin.org/247564 (Chrome debug - Type F12 in your Chrome) I do not know what would I do without these tools. ---------------------------------------------------------------------- BigChimp · 2013-02-21 14:53 Nice I still have the feeling you are talking about the server. I am talking about the addressbook client demo program included with Brook. It seems to ignore 404 responses by the server, as fa as I can tell by the code. What about changing this: brookframework\demos\db\rest\addressbook\frontend\frmcustomedit.pas Code: [Select]class function TfrCustomEdit.ProcessRequest(   const AHttpResult: THttpResult): Boolean; begin   Result := (AHttpResult.Code = 200) or (AHttpResult.Code = 201) or     (AHttpResult.Code = 204) or (AHttpResult.Code = 404);   if not Result then     ShowMessageFmt('ERROR: Text: %s; code: %d.',       [AHttpResult.Text, AHttpResult.Code]); end; to this: Code: [Select]class function TfrCustomEdit.ProcessRequest(   const AHttpResult: THttpResult): Boolean; begin   Result := (AHttpResult.Code = 200) or (AHttpResult.Code = 201) or     (AHttpResult.Code = 204); // <------------- NO MORE 404!!!!!!!!!!!!!!!!ELEVEN!!!!!!!!!!!!ONE!!!!!   if not Result then     ShowMessageFmt('ERROR: Text: %s; code: %d.',       [AHttpResult.Text, AHttpResult.Code]); end; ... so the end user gets to see the error if the server URLs don't match anymore? ---------------------------------------------------------------------- Silvio Clécio · 2013-02-21 16:42 Ah, now I understood. REST return 404 if the resource don't exists. 404 for REST isn't error, but a status code. The addressbook demo implement it (like it http://phprestsql.sourceforge.net). To test it is very easy, follow steps below: 1. Comment your 404 code: Code: [Select]class function TfrCustomEdit.ProcessRequest(   const AHttpResult: TBrookHTTPResult): Boolean; begin   Result := (AHttpResult.StatusCode = 200) or (AHttpResult.StatusCode = 201) or     (AHttpResult.StatusCode = 204);// or (AHttpResult.StatusCode = 404);   if not Result then     ShowMessageFmt('ERROR: Text: %s; code: %d.',       [AHttpResult.ReasonPhrase, AHttpResult.StatusCode]); end; 2. Run the demo and add a new contact; 3. Now, select the new contact added; In step 3 it will return a 404 error, but if you keep the "or (AHttpResult.StatusCode = 404)" it work correctly. So, the demo can be changed to (please test it via GIT copy): Code: [Select]class function TfrCustomEdit.ProcessRequest(   const AHttpResult: TBrookHTTPResult): Boolean; begin   Result := AHttpResult.StatusCode = 404;   if Result then   begin     ShowMessage('No record(s).');     Exit;   end;   Result := (AHttpResult.StatusCode = 200) or (AHttpResult.StatusCode = 201) or     (AHttpResult.StatusCode = 204);   if not Result then     ShowMessageFmt('ERROR: Text: %s; code: %d.', [AHttpResult.ReasonPhrase,       AHttpResult.StatusCode]); end; ---------------------------------------------------------------------- BigChimp · 2013-02-22 05:09 Thanks a lot, Silvio, now I understand ---------------------------------------------------------------------- Silvio Clécio · 2013-02-22 11:14 Fine.