You need to urlencode
your text_
object. In your example, text_
is university south
, which has a space in it. This introduces a space into your generated URL for the API call, so only https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=ba82bd4e0b4a41003b070a509184bce6&text=university
is getting sent. Without the format_
variable being added to it, the API call returns its default format - XML.
Flickr never returns a JSON only in Django, I'm getting XML instead
Pregunta
I don't know why but when I have a Django server make this very API call:
and when I then try this python code:
urlopen(strurl)
The first quarter (the rest is cut off since it's very long) of the response looks like this:
<?xml version="1.0" encoding="utf-8" ?>\n<rsp stat="ok">\n<photos page="1" pages="95250" perpage="100" total="9524986">\n\t
<photo id="14116658696" owner="48199179@N03" secret="3146e477c7" server="7457" farm="8" title="M\xfasica en la Pradera_94" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="13953203080" owner="48199179@N03" secret="8de6538ae3" server="7300" farm="8" title="M\xfasica en la Pradera_93" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="13953174029" owner="48199179@N03" secret="b7ccd34eef" server="7358" farm="8" title="M\xfasica en la Pradera_92" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="14116662786" owner="48199179@N03" secret="90a0b2fa15" server="7303" farm="8" title="M\xfasica en la Pradera_95" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="14159930793" owner="48199179@N03" secret="6a025d26a5" server="7399" farm="8" title="M\xfasica en la Pradera_97" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="13953195960" owner="48199179@N03" secret="f95af1aa0f" server="5555" farm="6" title="M\xfasica en la Pradera_91" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="14116666766" owner="48199179@N03" secret="f537c606d5" server="7414" farm="8" title="M\xfasica en la Pradera_96" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="13953114697" owner="32631656@N03" secret="51e5a538fa" server="5236" farm="6" title="Library Research Prize 2014" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="14136877152" owner="95437739@N07" secret="0b0ae018aa" server="7353" farm="8" title="Nurse Pinning Ceremony May 7, 2014" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="14159875503" owner="95437739@N07" secret="2b30819b0d" server="5596" farm="6" title="Nurse Pinning Ceremony May 7, 2014" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="14116517856" owner="19531332@N03" secret="948f316247" server="7439" farm="8" title="A pair of Highliners" ispublic="1" isfriend="0" isfamily="0" />\n\t
<photo id="13952995268" owner="48007774@N03" secret="ef093b35b6" server="7408" farm="8" title="Senior Day 2014" ispublic="1" isfriend="0" isfamily="0" />\n\t
Clearly that's not what I asked for, and if you click the URL above which'll open that very API call, you'll get the correct output in JSON, however when that exact same url is opened using urlopen()
ONLY IN DJANGO the response for some reason comes back as XML.
The XML code you see above is apart of the
ValueError: No JSON object could be decoded
Django error stack trace which is clearly stating that strurl is 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=ba82bd4e0b4a41003b070a509184bce6&text=university south&lat=37.4451302&lon=-122.1561432&format=json&nojsoncallback=1'
.
Somehow gets an XML response instead. Flickr's default format for API responses is XML, which seems to be ignoring in this Django app the json&nojsoncallback=1
At first I wrote this script in pure Python and this issue has only come up once I added it to this Django view:
def get_location_image(request):
text_ = request.GET.get('text','')
lat_ = request.GET.get('lat','')
long_ = request.GET.get('long','')
API_key = 'ba82bd4e0b4a41003b070a509184bce6' #API key is not mine, its public
url_ = 'https://api.flickr.com/services/rest/?method=flickr.photos.search'
format_ = 'json&nojsoncallback=1'
strurl = url_+'&api_key='+API_key+'&text='+text_+'&lat='+lat_+'&lon='+long_+'&format='+format_
request = urlopen(strurl)
response = request.read().decode("utf-8")
data = json.loads(response)
The Django app crashes as soon as it gets to data = json.loads(response)
with the exception: 'No JSON object could be decoded'.
Should I not be doing all this within a Django view and add it to a separate .py file instead and then import it to views.py?
Solución