Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
auzy
Binge Watcher

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
0 Kudos
9 REPLIES 9
RokuJoel
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

Please see ifURLTransfer:

RetainBodyOnError(retain as Boolean) as Boolean

If retain is true, return the body of the response even if the HTTP status code indicates that an error occurred.

- Joel
auzy
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

Thank you Joel, that looks to be exactly what I need; I'll check it out.

How about the 65536 char max string size (64KB)? Is that indeed the max readable size of the response or is there a way to handle longer responses?

Thanks
0 Kudos
RokuMarkn
Visitor

Re: reading roUrlTransfer response body when HTTP error rece

I haven't tested it, but I'm pretty sure the 64K limitation applies only to AsyncGetToString. You can read longer responses by using AsyncGetToFile. You would then of course have to read the response from the file.

--Mark
0 Kudos
RokuJoel
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

I've used AsyncGettoString where really large data sets come across and haven't had it error out on me. If this was an actual limitation, then many RSS based channels would not work at all.

- Joel
0 Kudos
auzy
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

"RokuJoel" wrote:
I've used AsyncGettoString where really large data sets come across and haven't had it error out on me. If this was an actual limitation, then many RSS based channels would not work at all.

- Joel

That's great to know, thanks. I'll stick with AsyncGettoString then unless I see a problem.
Why do you think that max size is listed in the docs? Was it just a provisionally planned limitation?
0 Kudos
auzy
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

What minimum firmware version is RetainBodyOnError(retain As boolean) supported on? It looks like it works fine on v4.x+, but I have an older model running FW 3.1 that doesn't seem to support it. The PDf docs don't list the method, and the online docs don't list what version the methods were introduced in.

Member function not found in BrightScript Component or interface.

113: http.Http.RetainBodyOnError(true)


Thanks!
0 Kudos
auzy
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

"auzy" wrote:
What minimum firmware version is RetainBodyOnError(retain As boolean) supported on? It looks like it works fine on v4.x+, but I have an older model running FW 3.1 that doesn't seem to support it. The PDf docs don't list the method, and the online docs don't list what version the methods were introduced in.

(bump)
Can one of the mods please check into this? Thanks.
0 Kudos
RokuJoel
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

I believe this is supported in 4.9 and later firmware.

- Joel
0 Kudos
auzy
Binge Watcher

Re: reading roUrlTransfer response body when HTTP error rece

I believe this is supported in 4.9 and later firmware.

Thanks, Joel.

Does that mean that there's absolutely no way to get the body of an HTTP response if the response code isn't in the 20x range? Any other work-arounds or tricks?

We have a RESTful API that returns specific and relevant error messages on 40x responses that need to be shown to the user.
Otherwise, unfortunately, when we publish our updated channel we will have to cut off support for FW < 4.9 😞
0 Kudos