0

I am attempting to retrieve information around a book via the good reads API. So far, I've attempted the following:

  1. Through Postman: A simple get request with the API key and query string works perfectly, and I receive an XML response: enter image description here

2.Through django/python methods : I wrote a simple method that uses the 'requests' package to retrieve data as follows, this also works with info being returned as expected:

def get_goodreads_response(book_name):

    api_key = config('GOODREADS_API_KEY')
    response = requests.get('https://www.goodreads.com/book/title.xml?key=' + api_key + '&title=' + book_name)
    response_xml = ElementTree.fromstring(response.content)
    return response_xml
  1. Through the axios JS library in a django template: I am very new to JS, and found snippets of code over the web that I've put together for now, this is what it looks like:
<script>
    axios.get('https://www.goodreads.com/book/title.xml?key={{ api_key }}&title={{ book_name }}')
    .then(response => {
        console.log(response.data)
    })
    .catch(error => console.error(error))
</script>

This throws the following error: enter image description here

I haven't the slightest clue about where to begin to debug this. Can you tell me where I'm going wrong? The reason I am trying to do this with javascript is to allow the rest of the page to load and not be delayed by the API call. This way, only the sections that are dependent on the API will take time to load and the rest of the content can come up quickly. Currently, I'm just trying to successfully call the API with Javascript and get a response first. Once this works, I plan to move to loading parts of the page as data is available. If you all have any suggestions on this approach please do share!

  • The api documentation on their website talks about getting the data with `curl`. It doesn't really say anything about ajax, as far as I can see. – Taplar Aug 26 '20 at 18:11
  • 1
    Probably need to use a proxy either on your server or third party service to get the data from – charlietfl Aug 26 '20 at 18:12
  • @charlietfl i tried the answer marked on this post: [link]https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141[link] - this works and i get the information i request. But I am wondering if this is a good way to work around this in production? – Dhruva Pasricha Aug 26 '20 at 18:26
  • 1
    Personally I would create my own proxy so I have control over it. They aren' complicated...simply a matter of making ajax request to your endpoint and it makes server to server request for the resource and returns it to your ajax – charlietfl Aug 26 '20 at 18:30
  • I'm using heroku to set up my own proxy and will use it similarly. I guess that should work well enough – Dhruva Pasricha Aug 26 '20 at 18:36

0 Answers0