reading roUrlTransfer response body when HTTP error received

Hi,

I’m going through some of the demos and modifying them as needed to do some early testing, and I came across a hurdle I need to get clear.
In the urlUtils.brs source file that’s included in some demos, there’s a method http_get_to_string_with_retry(), which basically just wraps the calling and handling of AsyncGetToString().

However I found that when an HTTP “error” occurs, e.g. the response code is in the 40x range, the event.getString() returns empty. Now, I have an API I’m trying to interface to that has important and appropriate information in the body of such responses, especially 401 Unauthorized, 403 Forbidden, etc, and there is text within the body of those responses I need to be able to parse so that I can present them to the user.

  1. How do I get the body of the response for all HTTP responses, not just those that have HTTP response codes in the 200 range?
  2. The docs say that roUrlEvent.getString() only returns up to 65536 characters for “…AsyncGetToString, AsyncPostFromString and AsyncPostFromFile requests…”. Am I reading that wrong, or does that imply that the XML responses I’m trying to retrieve are limited to 65536 chars?

Thanks!

Here’s the code for the method I was talking about:

Function http_get_to_string_with_retry() as String
    timeout%         = 1500
    num_retries%     = 5

    str = ""
    while num_retries% > 0
'        print "httpget try " + itostr(num_retries%)
        if (m.Http.AsyncGetToString())
            event = wait(timeout%, m.Http.GetPort())
            if type(event) = "roUrlEvent"
                str = event.GetString()
                print "body:"
                print str
                exit while        
            else if event = invalid
                m.Http.AsyncCancel()
                REM reset the connection on timeouts
                m.Http = CreateURLTransferObject(m.Http.GetUrl())
                timeout% = 2 * timeout%
            else
                print "roUrlTransfer::AsyncGetToString(): unknown event"
            endif
        endif

        num_retries% = num_retries% - 1
    end while
    
    return str
End Function

For anyone still searching for a solution, you need to set this in your code before sending the request to receive the response body when the response code is not 200. (Client error 4xx or Server error 5xx)

m.Http.RetainBodyOnError(true)