Elektra
Elektra

Reputation: 23

Django JSONDecodeError at ...= get_response(request)

When i run the server and look at localhost i got this error:

JSONDecodeError at ...Traceback response = get_response(request)

What am i doing wrong?

My Code is :

from django.shortcuts import render
import json
import requests


def home(request):
    response = requests.get('https://api.ipify.org')
    data = response.json()
    dump = json.dumps(data)
    return render(request, 'catalog/home.html', {'ip': dump})

Upvotes: 2

Views: 532

Answers (2)

Elektra
Elektra

Reputation: 23

Thank you all! blhsing gave my what i tried to do. Now i got the right format. The output was with {...} and i got the format like https://www.webforefront.com/static/images/beginningdjango/Figure_12-1.png ... what i need. i will try this for my other APP.

I will also try this: (with the same adress blhsing gave me and it works too. Only the output changed) return HttpResponse(dump,content_type='application/json')

...that i can make a GET request with json and the APP knows the format.

Upvotes: 0

blhsing
blhsing

Reputation: 106553

Change the URL to https://api.ipify.org?format=json and your code would work.

Alternatively, you can still use the https://api.ipify.org URL, but since it outputs the IP directly, you should skip the JSON parsing and return render(request, 'catalog/home.html', {'ip': response.content}) instead.

Upvotes: 1

Related Questions