Search code examples
javascriptjsonxmlhttprequestthemoviedb-api

JSON data cannot be accessed getting error 404 (TMDB)?


I want the poster of required movie from TMDB. I am not able to get JSON data from TMDB API. I have send the request but getting error 404 'The resource you requested could not be found'. Link to access TMDB movie API : https://developers.themoviedb.org/3/movies/get-movie-images Here is my code:

<script type="text/javascript">
    var film = "speed";
    var api_key = 'my-api-key';
    var requestURL = "https://api.themoviedb.org/3/movie/images?api_key=" + api_key +"&language=en-US&callback=?";

    var request = new XMLHttpRequest();

    request.open('GET', requestURL);

    request.responseType = 'json';

    request.send();

    request.onload = function(){
        var myjsondata = request.response; //request.response contains all our JSON data 

        console.log(myjsondata);
    } 
</script>

The JSON data in my console should look like this: enter image description here

But instead I am getting this in my console:error 404 This resource cannot be found.


Solution

  • You need to include a movie_id value in the path part of the request URL, right? Like this:

    var requestURL = "https://api.themoviedb.org/3/movie/"
      + movie_id + "/images?api_key=" + api_key +"&language=en-US&callback=?";
    

    At least in the documentation cited in the question that’s what’s shown:

    GET /movie/{movie_id}/images

    Path Parameters

    movie_id : integer
    

    Example:

    https://api.themoviedb.org/3/movie/{movie_id}/images?api_key=<<api_key>>
    

    For example, to get JSON-formatted data for the images for the movie with the ID 9340:

    https://api.themoviedb.org/3/movie/9340/images?api_key=<<api_key>>
    

    You can confirm that works by testing with curl or whatever:

    $ curl "https://api.themoviedb.org/3/movie/9340/images?api_key=<<api_key>>"
    
    {
        "backdrops": [
            {
                "aspect_ratio": 1.777251184834123,
                "file_path": "/qKeyO9gXaaK0g87tvvqOPK1siwc.jpg",
                "height": 1688,
                "iso_639_1": null,
                "vote_average": 5.454545454545455,
                "vote_count": 3,
                "width": 3000
            },
            …