I’ve been racking my brain, trying to figure out where my code was faulty, or at least where the API was inadequate, and it took me several hours to realize that HTTP redirection is transparent. In many cases I’m sure that’s acceptable, but in my case it makes it pretty much impossible to finish my plugin without utilizing functions outside the Plex API. It just so happens I need the URL that my request is being redirected to because that’s the URL I need to trigger my URL Service. In my case, it would work just as well to add the retrieved URL as an attribute of HTTPRequest, but disabling automatic redirects can cut down on I/O too if the developer doesn’t wish to follow the redirect.
I’m hoping the devs will implement my suggestion or some acceptable alternative in a future release, until then I’m contemplating modifying the Framework.bundle as to not follow redirects or simply use urllib2 in my code.
It’s already there
Sorry, it probably isn’t documented yet - updating the docs on the dev site is on my list of things to do, just haven’t got round to it yet. If you find the framework lacking in any other way, please post here and if there’s not already a way of doing what you need we’ll look in to adding it
Anyway… any HTTP-requesting method (e.g. HTTP.Request, XML.ElementFromURL, etc.) accepts a follow_redirects boolean attribute. By default, this is set to True. If you pass in False, the framework will raise Ex.RedirectError if a redirect is encountered. The raised exception object will have a few properties: “code” is set to the HTTP status code (either 301 or 302 depending on the redirect type) and “headers” is a dict containing the returned HTTP headers. There’s also a “location” convenience property for accessing the location header.
Oh, excellent! I probably would’ve noticed that after digging through the code but not before many hours of drinking and swearing . So something like this should work (not at home to test it)?
<br />
try:<br />
req = HTTP.Request(url, follow_redirects = False)<br />
except Ex.RedirectError, e:<br />
if (e.code == 302):<br />
url2 = req.location # the new redirected URL???<br />
Yes, although you probably don’t need to check the code unless you need to differentiate between temporary & permanent redirects. Apologies for the hours of swearing
Oops!
Nevermind.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.