I am attempting to retrieve information around a book via the good reads API. So far, I've attempted the following:
- Through Postman: A simple get request with the API key and query string works perfectly, and I receive an XML response:
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
- 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:
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!