C/AL, NAV2016, HttpWebRequest, processing fault response

northyennorthyen Member Posts: 14
How you do get the fault response InStream (or for that sake, file) from the HttpWebRequest, without modifying C1297?

NAV is posting to a webservice that returns status 422, which means that there are errors in the data supplied. I'm trying to find a way to get this information for further processing. Right now I just get the status "422 Unprocessable Entity", but not the response body.

It seems that SetTraceLogEnabled actually does this, but I'm unable to get the file name, and I'd prefer a stream anyhow.

Answers

  • ShaiHuludShaiHulud Member Posts: 228
    Codeunit 1299 has a nice function for inspiration: "GetWebResponseError". However, personally, I didn't find it fully suitable for my needs, so I used parts of it and made my own handling that looks something like this:
    IF NOT TryAPIRequest(HttpWebRequest_L,HttpWebResponse_L) THEN BEGIN //TryAPIRequest is a simplified version of COD1297's "GetWebResponse"
      DotNetExceptionHandler.Collect;
      IF NOT DotNetExceptionHandler.CastToType(WebException_L,GETDOTNETTYPE(WebException)) THEN
        DotNetExceptionHandler.Rethrow;
      IF ISNULL(WebException.Response) THEN
        DotNetExceptionHandler.Rethrow;
      HttpWebResponse := WebException.Response;
      ResponseStream := HttpWebResponse.GetResponseStream();
    

    DotNetExceptionHandler: COD1291
    Find other types in COD1299.GetWebResponseError
  • northyennorthyen Member Posts: 14
    edited 2019-03-22
    I found the solution late last night actually, that fits my needs. I got completely confused about the "WebException" part as it seem that the DotNet exceptions actually lives in some kind of super-global scope.

    I.e. I don't have to think about scopes, parameters, references or anything like that. Confusing!
    IF NOT HttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders) THEN BEGIN
      FaultResponse := WebRequestHelper.GetWebResponseError(WebException, ServiceURL);
    
      IF NOT ISNULL(WebException.Response) THEN BEGIN
        Instr := WebException.Response.GetResponseStream;
        StreamToLog(Instr,"Function" + ' Response');
      END;
    END;
    
Sign In or Register to comment.