2

I have a problem: I am developing an API view in django that must respond to remote requests.

Is there a way to know what is the url from which the API call starts?

Thank you

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92

1 Answers1

0

Yes, in the HTTP headers, there will be a Referer line, which is the URI from which the client came, if they choose to send it. Some people choose to do things so it isn't sent, and in that case, there isn't much you can do about it.

There are several posts about parsing HTTP headers floating around, you can just use one of those and borrow their methods.

This is one of the posts that has several good examples in it: Parse raw HTTP Headers

You just need to make Django snag the headers so you can parse them, which isn't difficult to do, I believe there are functions in the standard library for getting headers in web apps.

Community
  • 1
  • 1
Seth Curry
  • 151
  • 4
  • Yes the point is that in some requests this header appears, but in other requests no. So the solution is force sending? And how can i get the http_referer to send ? Thank you so much – Domenico Schioppa May 09 '13 at 12:53
  • Right, if that header isn't there, there isn't a good way to figure out where the call came from other than if they came from somewhere on your site, you could look through the site records. If they came from elsewhere or directly entered the call into a program, or otherwise don't send the header, you don't have a good way of knowing where they came from. – Seth Curry May 09 '13 at 12:55
  • and how can i get a correct value for http_referer ti send it? In my case the api call is made in a view, using urllib – Domenico Schioppa May 09 '13 at 12:57