2

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.

rock stone
  • 473
  • 2
  • 9
  • 20
  • It tells you that the resource with that `api_key` doesn't exist on the server. Try it manually in your browser or other tool to see if you really get something back. – Edwin Jun 07 '17 at 07:31
  • @RDhaval I am getting invalid API key when I substitute requestURL with the one you mentioned above. – rock stone Jun 07 '17 at 08:16
  • @RDhaval join in discussion I have sent you messages. – rock stone Jun 07 '17 at 09:04

1 Answers1

3

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
        },
        …
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • what should I substitute for movie_id and is it necessary as in documentation it is written optional. – rock stone Jun 07 '17 at 07:46
  • Try `9340`. That’s the number in the movie URL shown in the image at https://i.stack.imgur.com/LeotR.png. So I assume that’s the movie ID. – sideshowbarker Jun 07 '17 at 07:48
  • it is mentioned that movie_id is optional – R Dhaval Jun 07 '17 at 07:50
  • I have no idea why the docs say the movie_id is optional in that API call. But if you’re trying to get a poster image for a particular movie, how else would you imagine you’d identify which movie you want a poster if you don’t specify the movie ID in that API call? – sideshowbarker Jun 07 '17 at 07:52
  • @sideshowbarker ok but how will I get that JSON data I am not able to get that JSON data that's my question and I also tried 9340 as movie_id but now I get null instead of error 404. – rock stone Jun 07 '17 at 07:53
  • @sideshowbarker will you please look the code on this site-> https://www.smashingmagazine.com/2012/02/beginners-guide-jquery-based-json-api-clients/ I am trying to do the same here. – rock stone Jun 07 '17 at 07:54
  • @RDhaval will you please look into the code on this site ->https://www.smashingmagazine.com/2012/02/beginners-guide-jquery-based-json-api-clients/ I am trying to do same here – rock stone Jun 07 '17 at 07:56
  • @rockstone See the update I just added to the end of the answer. `https://api.themoviedb.org/3/movie/9340/images?api_key=<>` returns a valid JSON response, including an Access-Control-Allow-Origin response header that ensures browsers will allow you frontend JavaScript code to access the response cross-origin. So if you’re getting null then there’s something else wrong with your code that you need to fix. – sideshowbarker Jun 07 '17 at 08:08