JSONDecodeError

65 views
Skip to first unread message

Yash Garg

unread,
Jan 6, 2020, 8:20:19 AM1/6/20
to Django users
JSONDecodeError
Exception Value:
Expecting value: line 1 column 1 (char 0)

why i'm getting this error?

views.py-

ip_address = request.META.get('HTTP_X_FORWARDED_FOR', '')
response = requests.get('http://freegeoip.net/json/%s' % ip_address)
geodata = json.loads(response)
print(geodata['ip'])
print(geodata['country_name'])
print(geodata['latitude'])
print(geodata['longitude'])

Integr@te System

unread,
Jan 6, 2020, 10:58:50 AM1/6/20
to django...@googlegroups.com
Hi,

Maybe your module requests not present sufficient and check requests api module to use.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/34b7736d-f946-451b-b83b-de72d3329da0%40googlegroups.com.

Integr@te System

unread,
Jan 6, 2020, 11:08:21 AM1/6/20
to django...@googlegroups.com
or another way django httpxforwardedfor module.
Nice.

Mohamed A

unread,
Jan 6, 2020, 11:10:21 AM1/6/20
to django...@googlegroups.com
According to the documentation 

If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.

however the object response, which it of type "requests.models.Response" n'est pas serializable according to the conversion table (mentioned in doc).

A solution would be to use
geodata = response.json()
 

Reply all
Reply to author
Forward
0 new messages