0

I'm developing an Android app (Client). The server works with C#. When I make some request specifically some Posts, I'm getting an error message on bad request such as 'HTTP/1.1 404 Not Found' which is okay when the item I searched for is incorrect. But on bad request the server sends me also a message body in JSON, something like this:

  {
    "responseMessage":"The item you searched not found"
  }

Is there a way to get this message (and NOT the bad request message 'HTTP/1.1 404 Not Found') and show it as a response of bad request? The back end works fine, I check it on Postman. My code of Post request is this:

  Json := '{ '+
          ' "code":"'+str+'",'+
          ' "userid":'+userid+''+
          ' } ';
  JsonToSend := TStringStream.Create(Json, TEncoding.UTF8);
  try
   IdHTTP1.Request.ContentType := 'application/json';
   IdHTTP1.Request.CharSet := 'utf-8';

   try

   sResponse := IdHTTP1.Post('http://....', JsonToSend);

   except      
       on e  : Exception  do
        begin
             ShowMessage(e.Message); // this message is : HTTP/1.1 404 Not Found
             Exit;
        end;

   end;

  finally
  JsonToSend.Free;
  end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Antonis
  • 55
  • 5
  • `HTTP 400` is `Bad Request`, while `HTTP 404 Not Found` exactly means that and not more - it doesn't imply a "_bad request_", nor when the requested resource will be available. Also the text must not be like that - you could also serve `HTTP 404 No posts found`. Also sure you want to request `http` and not `https`? – AmigoJack Mar 29 '22 at 14:42

1 Answers1

1

To receive the JSON content on errors, you have 2 options:

  1. Catch the raised EIdHTTPProtocolException and read the JSON from its ErrorMessage property, not the Message property:

    try
      sResponse := IdHTTP1.Post(...);
    except      
      on E: EIdHTTPProtocolException do
      begin
        sResponse := E.ErrorMessage;
      end;
      on E: Exception do
      begin
        ShowMessage(e.Message);
        Exit;
      end;
    end;
    
  2. Enable the hoNoProtocolErrorException and hoWantProtocolErrorContent flags in the TIdHTTP.HTTPOptions property to prevent TIdHTTP.Post() from raising EIdHTTPProtocolException on HTTP failures, and instead just return the JSON to your sResponse variable. You can use the TIdHTTP.ResponseCode property to detect HTTP failures, if needed:

    IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];
    
    try
      sResponse := IdHTTP1.Post(...);
      if IdHTTP1.ResponseCode <> 200 then ...
    except      
      on E: Exception do
        ...
    end;
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770