Is there a way to get the 400 respons message and the response JSON with .Net 4 in CSIDE?

connavconnav Member Posts: 1
Hey I use the following to send a json to a server. The problem is, when an wrong formatted JSON is send, i only get the Message "400 Invalid [...]" , while the server is sending the wrong JSON (with some notes) and the 400 errorcode back. Any ideas how i get the Json (the one with notes)?
// Authorization
HttpWebRequest := HttpWebRequest.HttpWebRequest;
HttpWebRequest :=  HttpWebRequest.Create('*URL From Server*');
HttpWebRequest.ContentType := 'application/json';
HttpWebRequest.Method := 'POST';
HttpWebRequest.Headers.Add('Authorization',BearerTok);

// Create JSON
AddBodyAsTextWithEncoding(HttpWebRequest,GetArticelExample, Encoding.UTF8);

// Get response
HttpWebResponse := HttpWebResponse.HttpWebResponse;
HttpWebResponse := HttpWebRequest.GetResponse;
HttpResStatus := HttpWebResponse.StatusCode();

// Read response
CLEAR(Response);
  ResStream := HttpWebResponse.GetResponseStream();
  ResStreamReader := ResStreamReader.StreamReader(ResStream,TextEncoding.UTF8);
  Response := ResStreamReader.ReadToEnd();
  


HttpWebResponse.Close; 
HttpWebResponse.Dispose;

Var
HttpWebRequest DotNet System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089';
HttpWebResponse DotNet System.Net.HttpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089';
ResStream DotNet System.IO.Stream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089';
HttpResStatus Integer ;

Answers

  • AlexDenAlexDen Member Posts: 86
    Hi,

    Put this line into TRY function:
    LOCAL [TryFunction] GetWebResponse(VAR HttpWebRequest : DotNet "System.Net.HttpWebRequest";VAR HttpWebResponse : DotNet "System.Net.HttpWebResponse")
    CLEARLASTERROR;
    HttpWebResponse := HttpWebRequest.GetResponse;
    

    Add function to catch an exception:
    GetWebResponseError(VAR HttpWebResponse : DotNet "System.Net.HttpWebResponse") : Text
    Exception : DotNet System.Exception.'mscorlib'	
    WebException: DotNet System.Net.WebException.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    ***
    Exception := GETLASTERROROBJECT;
    WebException := Exception.GetBaseException;
    IF ISNULL(WebException) THEN
      EXIT(Exception.Message);
    
    HttpWebResponse := WebException.Response;
    EXIT(WebException.Message);
    


    And handle an exception:
    IF NOT GetWebResponse(HttpWebRequest, HttpWebResponse) THEN
      ErrorMsgText := GetWebResponseError(HttpWebResponse);
    
    IF NOT ISNULL(HttpWebResponse) THEN BEGIN
      ResStream := HttpWebResponse.GetResponseStream();
      IF HttpWebResponse.StatusCode <> 200 THEN BEGIN
        IF ErrorMsgText = '' THEN
          ErrorMsgText := HttpWebResponse.StatusDescription;
        // Put your code to handle your exception
        ...
      END;
      ...
    END;
    

Sign In or Register to comment.