Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send http request using django and get results

This might seem like an extremely simple and stupid question yet i can't find a convenient answer.
I'm trying to use google's reverse geocodding api with django, as the website explains (at https://developers.google.com/maps/documentation/geocoding/start?csw=1#ReverseGeocoding) I'm supposed to send a request to the url :
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=<_api_key>
But I can't find an appropriate way to send this request using django to google api.
To me it seems something so simple will be doable by some method built-in django but all I could find was modules to install.
If there's no way but installing other python modules which one's the best?

like image 967
Bahman Rouhani Avatar asked Mar 11 '17 16:03

Bahman Rouhani


1 Answers

Well, the most straightforward way would be to install requests library and simply call:

result = requests.get(your_link)

You can find additional information (e.g., how to authenticate, use cookies, etc., as well as how to access data in response) in the library's docs. The requests library is very well written, very intuitive and simple to use. Quite a lot of smart people and companies use it, too, so it's not a half-baked library someone just hacked out over the weekend (as of this moment, it has 6060 commits and 595 contributors on GitHub).

If you absolutely must avoid external libraries, why not try urllib.request. It's a bit more complicated, and even the docs themselves recommend using requests if you prefer higher-level interface. But you definitely can get the job done with it. To get started, you can read the docs on how to use it to fetch data. Read this thread for an example of how to extract json from a response you'd get with urllib.

like image 180
cegas Avatar answered Oct 03 '22 21:10

cegas