How to handle errors for HTTPWebRequest.GetResponse call?

When HTTPWebRequest.GetResponse is called and no proper response is returned from the server, the code stops executing and returns some random error. I want to implement proper error handling, so the code keeps on executing without stopping here and a proper message is displayed to the user. Like we apply checks on status code when we use automation objects.
Any help in this regard would be appreciated.

Best Answer

  • ftorneroftornero Member Posts: 522
    Answer ✓
    Hello @azharsaeedkhan

    In NAV 2013 that don't have a try function I do something like this
    ASSERTERROR BEGIN
      Response := Request.GetResponse();
      IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN
        ResponseStream := Response.GetResponseStream();
        ERROR('OK');
      END ELSE BEGIN
        HttpWebStatus := Response.StatusCode;
        ERROR(Response.StatusDescription);
      END;
    END;
    IF GETLASTERRORTEXT = 'OK' THEN BEGIN
      IF NOT ISNULL(ResponseStream) THEN BEGIN
    .....
    

    Regards.

Answers

  • irasoelbaksirasoelbaks Member Posts: 119
    The function GetResponse already returns a Boolean but I think you already know that. You could do something like:

    Pseudo code:
    IF NOT HttpWebRequest.GetResponse THEN ERROR('')

    I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.

    Pseudo code:
    if HttpStatusCode = 200 THEN
    EXIT(True) ELSE // Success
    ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong

  • azharsaeedkhanazharsaeedkhan Member Posts: 34
    > @irasoelbaks said:
    > The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
    >
    > Pseudo code:
    > IF NOT HttpWebRequest.GetResponse THEN ERROR('')
    >
    > I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
    >
    > Pseudo code:
    > if HttpStatusCode = 200 THEN
    > EXIT(True) ELSE // Success
    > ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong

    It doesn't return a boolean, but HttpWebResponse object.
    The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
    But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
  • ftorneroftornero Member Posts: 522
    Answer ✓
    Hello @azharsaeedkhan

    In NAV 2013 that don't have a try function I do something like this
    ASSERTERROR BEGIN
      Response := Request.GetResponse();
      IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN
        ResponseStream := Response.GetResponseStream();
        ERROR('OK');
      END ELSE BEGIN
        HttpWebStatus := Response.StatusCode;
        ERROR(Response.StatusDescription);
      END;
    END;
    IF GETLASTERRORTEXT = 'OK' THEN BEGIN
      IF NOT ISNULL(ResponseStream) THEN BEGIN
    .....
    

    Regards.
  • irasoelbaksirasoelbaks Member Posts: 119
    > @irasoelbaks said:
    > The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
    >
    > Pseudo code:
    > IF NOT HttpWebRequest.GetResponse THEN ERROR('')
    >
    > I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
    >
    > Pseudo code:
    > if HttpStatusCode = 200 THEN
    > EXIT(True) ELSE // Success
    > ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong

    It doesn't return a boolean, but HttpWebResponse object.
    The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
    But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
    I think you are looking at some other function? What function are you looking at because the returnval of the function is Boolean for sure:
    8xhne67ci2t4.png


  • irasoelbaksirasoelbaks Member Posts: 119
    edited 2021-03-12
    ftornero wrote: »
    Hello @azharsaeedkhan

    In NAV 2013 that don't have a try function I do something like this
    ASSERTERROR BEGIN
      Response := Request.GetResponse();
      IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN
        ResponseStream := Response.GetResponseStream();
        ERROR('OK');
      END ELSE BEGIN
        HttpWebStatus := Response.StatusCode;
        ERROR(Response.StatusDescription);
      END;
    END;
    IF GETLASTERRORTEXT = 'OK' THEN BEGIN
      IF NOT ISNULL(ResponseStream) THEN BEGIN
    .....
    

    Regards.

    Please notice that an API can also return HTTP 201 for example. So only checking for OK is not (always) good enough.
  • Gardner32Gardner32 Member Posts: 1
  • azharsaeedkhanazharsaeedkhan Member Posts: 34
    ftornero wrote: »
    Hello @azharsaeedkhan

    In NAV 2013 that don't have a try function I do something like this
    ASSERTERROR BEGIN
      Response := Request.GetResponse();
      IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN
        ResponseStream := Response.GetResponseStream();
        ERROR('OK');
      END ELSE BEGIN
        HttpWebStatus := Response.StatusCode;
        ERROR(Response.StatusDescription);
      END;
    END;
    IF GETLASTERRORTEXT = 'OK' THEN BEGIN
      IF NOT ISNULL(ResponseStream) THEN BEGIN
    .....
    

    Regards.

    Thank you @ftornero this is very helpful
  • azharsaeedkhanazharsaeedkhan Member Posts: 34
    > @irasoelbaks said:
    > The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
    >
    > Pseudo code:
    > IF NOT HttpWebRequest.GetResponse THEN ERROR('')
    >
    > I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
    >
    > Pseudo code:
    > if HttpStatusCode = 200 THEN
    > EXIT(True) ELSE // Success
    > ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong

    It doesn't return a boolean, but HttpWebResponse object.
    The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
    But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
    I think you are looking at some other function? What function are you looking at because the returnval of the function is Boolean for sure:
    8xhne67ci2t4.png


    @irasoelbaks i am looking at this function
    kacbnqnr5ayy.png

    wubeg1ekqg4f.png

    Please let me know, which GetResponse function you are talking about.
  • azharsaeedkhanazharsaeedkhan Member Posts: 34
    ftornero wrote: »
    Hello @azharsaeedkhan

    In NAV 2013 that don't have a try function I do something like this
    ASSERTERROR BEGIN
      Response := Request.GetResponse();
      IF (HttpWebStatus.Equals(Response.StatusCode,HttpWebStatus.OK)) THEN BEGIN
        ResponseStream := Response.GetResponseStream();
        ERROR('OK');
      END ELSE BEGIN
        HttpWebStatus := Response.StatusCode;
        ERROR(Response.StatusDescription);
      END;
    END;
    IF GETLASTERRORTEXT = 'OK' THEN BEGIN
      IF NOT ISNULL(ResponseStream) THEN BEGIN
    .....
    

    Regards.

    Please notice that an API can also return HTTP 201 for example. So only checking for OK is not (always) good enough.

    Yes you are right, but we can handle that in code :)
  • azharsaeedkhanazharsaeedkhan Member Posts: 34
    > @irasoelbaks said:
    > The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
    >
    > Pseudo code:
    > IF NOT HttpWebRequest.GetResponse THEN ERROR('')
    >
    > I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
    >
    > Pseudo code:
    > if HttpStatusCode = 200 THEN
    > EXIT(True) ELSE // Success
    > ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong

    It doesn't return a boolean, but HttpWebResponse object.
    The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
    But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
    I think you are looking at some other function? What function are you looking at because the returnval of the function is Boolean for sure:
    8xhne67ci2t4.png


    @irasoelbaks i am looking at this function
    kacbnqnr5ayy.png

    wubeg1ekqg4f.png

    Please let me know, which GetResponse function you are talking about.

    @irasoelbaks i think you are talking about GetResponse function in codeunit 1297 Http Web Request Mgt. but i am working on NAV 2013 and it doesn't have this codeunit and try functions.
    Sorry my bad, i should have mentioned NAV version in my question
  • irasoelbaksirasoelbaks Member Posts: 119
    > @irasoelbaks said:
    > The function GetResponse already returns a Boolean but I think you already know that. You could do something like:
    >
    > Pseudo code:
    > IF NOT HttpWebRequest.GetResponse THEN ERROR('')
    >
    > I don't know what you mean with 'proper response' but when a request is succesfull you normally receive a HTTP 200. This value is returned as reference through HttpStatusCode. You can test the StatusCode output to identify if the response is OK, Unauthorized, Internal Server Error, Bad Request etc.
    >
    > Pseudo code:
    > if HttpStatusCode = 200 THEN
    > EXIT(True) ELSE // Success
    > ERROR('Something went wrong %1',HttpStatusCode); // Something went wrong

    It doesn't return a boolean, but HttpWebResponse object.
    The HttpWebResponse.StatusCode can only be called after the HttpWebRequest.GetResponse call, which initializes the HttpWebResponse object.
    But i am getting errors on HttpWebRequest.GetResponse call so i cannot check the status code at this point.
    I think you are looking at some other function? What function are you looking at because the returnval of the function is Boolean for sure:
    8xhne67ci2t4.png


    @irasoelbaks i am looking at this function
    kacbnqnr5ayy.png

    wubeg1ekqg4f.png

    Please let me know, which GetResponse function you are talking about.

    @irasoelbaks i think you are talking about GetResponse function in codeunit 1297 Http Web Request Mgt. but i am working on NAV 2013 and it doesn't have this codeunit and try functions.
    Sorry my bad, i should have mentioned NAV version in my question

    No problem. I meant Codeunit 1297 indeed.
Sign In or Register to comment.