1

I would like to invoke an external REST API in Wagtail CMS. Let us assume the URL is:

http://dummy.restapiexample.com/api/v1/employees

I tried to follow a Django post which was doing almost the same. See: Proper way to consume data from RESTFUL API in django

I created services.py:

//services.py

import requests

def get_employees(page, request):
    url = 'http://dummy.restapiexample.com/api/v1/employees' 
    r = requests.get(url)
    employees = r.json()
    return employees

I added the following to models.py:

from . import services as services

class HomePage(RoutablePageMixin, Page):

    @route(r'^employees/$')
    def employees_page(self,request):
        employees = services.get_employees(self, request)
        return render(request,'home/employees.html',employees)

When I run this I get the following error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here is the stacktrace:

  File "/data/swag/home/models.py", line 53, in employees_page
    employees = services.get_employees(self, request)
  File "/data/swag/home/services.py", line 13, in get_employees
    employees = r.json()
  File "/home/user1/anaconda3/envs/wadmin/lib/python3.7/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/home/user1/anaconda3/envs/wadmin/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/home/user1/anaconda3/envs/wadmin/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/user1/anaconda3/envs/wadmin/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

It seems the Django code does not work in Wagtail and needs to be adapted. I spent several hours looking for an equivalent but no success. Really appreciate any help.

techie
  • 41
  • 5
  • This error indicates that the response returned by the API is not valid JSON. Are you sure the original Django code works for the URL you're testing against? – gasman Feb 29 '20 at 00:21
  • @gasman, the API [link](http://dummy.restapiexample.com/api/v1/employees) is a dummy example REST API. I am using it to learn how to invoke REST API from models.py. I examined the HTTP Response headers for that URL in Chrome and it showed `content-type:application/json; charset-utf-8`. However when I print `r.headers['content-type']` in Wagtail it displays `text/html; charset=iso-8859-1` and `r.encoding` displays `iso-8859-1`. I also tried setting the following: ` – techie Mar 02 '20 at 22:38
  • Tried the following: `headers = {'content-type':'application/json; charset-utf-8', 'encoding':'charset-utf-8' } r = requests.get(url, headers=headers)` but it still displays `text/html; charset=iso-8859-1` – techie Mar 02 '20 at 22:46
  • I could **not** get the above api to work. However, I was able to get the following api to work: ```url = 'https://api.github.com/events' ``` It would be great if someone could explain why the github REST API is different from the dummy REST API above. – techie Mar 07 '20 at 02:05

0 Answers0