0

I am trying to use Google Books API to get results for a book but although the book is available on Google Books website the API doesn't return results for its ISBN

The ISBNs I am searching for are: 9781595086228 - 1595086226

The API Request:

https://www.googleapis.com/books/v1/volumes?q=isbn:9781595086228&key=<API Key>

and

https://www.googleapis.com/books/v1/volumes?q=isbn:1595086226&key=<API Key>

The response:

{kind: "books#volumes",
totalItems: 0
}

The Book page on Google Books:

https://books.google.com.eg/books?id=nAmyDwAAQBAJ&dq=9781595086228

I want to know if there is a problem with my request format or if the problem is not related to my request.

1 Answers1

0

The data type of typeof() ISBNs in the fetched object is string, thus need to include it in the call as the same type - imbed it as a variable in the URL using backticks. See the comments in the code:

<!DOCTYPE html>
<html lang="en">
<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>test</title>

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
  
  <script 
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous">
  </script>

</head>

<body>   

  <form class="d-grid w-100 me-3" role="search">
    <input type="search" id="search" class="form-control" aria-label=" Search" autofocus>
  </form>

  <span id="ini">Looking for ISBN: 9781595086228 or 1595086226</span>
  <span id="results"></span>


<script>

document.getElementById("search").addEventListener("keypress", Search);

function Search (event) {

  if (event.key === "Enter") {
    event.preventDefault();
    var search = document.getElementById('search').value;
  }

  fetchApi(search)
}

function fetchApi (x) {

  url = `https://www.googleapis.com/books/v1/volumes?q=isbn:`+x+`&maxResults=40&startIndex=0`

  return fetch(url)
  .then((response) => response.json())
  .then((data) => {

    if (data.items) {
      //console.log(data)
      document.getElementById('results').innerHTML = `<h1>${data.items[0].volumeInfo.title}</h1>` + `<p>by ${data.items[0].volumeInfo.authors.join(", ")}</p>`;

z = data.items[0].volumeInfo.industryIdentifiers[0].identifier;
  
  console.log('typeofISBN:', typeof(z)); // API stores ISBN as a string
  console.log('typeofValue:', typeof(9781595086228)); // ISBN value directly would include it in the call as a number; thus include it as a sting in the call, which is possible using backticks as shown in the URL above
    }
  })
}
</script>


</body>
</html>

P.S if helps, please mark as answer.

ViBP
  • 1
  • 2
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Aug 15 '23 at 08:57