2

I'm having issues getting a URL pattern to work.

The URL is in the format of the following:

/API#access_token=<string>&expires_in=<timestamp>

I can't change the #access_token=&expires_in= part unfortunately, as this is outside of my control, and I simply have to just make my side of the code work.

I've tried a number of different patterns, a number of which are outlined below. This is my first Django project, and any advice, and pointers would be much appreciated.

url(r'^API#access_token=(?P<token_info>\w+)&expires_in(?P<time>\d+)$'
url(r'^API#(?P<tokens>\w+)$'
url(r'^API/#(?P<tokens>\w+)&(?P<expiration>\d+)$'
Stewart Polley
  • 224
  • 1
  • 2
  • 9

1 Answers1

7

The issue is that the anchor #, also called the fragment identifier, is not sent to the server by the browser. The regex cannot capture what is not there. From the wikipedia article on the fragment identifier:

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the web server — of course the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent (such as a Web browser) requests a web resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.

The only way around this is to parse the fragment in JavaScript on the client side and send it as a separate asynchronous request. For a GET request, you could send the fragment as a query parameter (after stripping off the hash) or put it in the header as a custom value.

Fiver
  • 9,909
  • 9
  • 43
  • 63
  • Is there any way to make the browser to send that back to the server after the HTML loads? Ie, load page first, browser reads URL and then send the fragment back? – Stewart Polley Feb 20 '15 at 09:22
  • Yes, you can parse client side and send a separate AJAX request. – Fiver Feb 20 '15 at 14:54