React应用部署Heroku后调用The Movie DB HTTPS API遇请求错误
Fixing Your React App's TMDB API Issues on Heroku
Hey David, let's tackle those two problems you're facing with your deployed React app—they're common gotchas, so we'll get them sorted out quickly!
1. Mixed Content (HTTPS Page vs HTTP API Request)
Even though you updated the base URI to HTTPS, it's possible there's still a stray HTTP request hiding somewhere. Here's how to track it down:
- Open your browser's DevTools (F12) and go to the Network tab. Refresh the page and look for any requests marked as "Not Secure"—this will show you exactly which API call is still using HTTP.
- Double-check all places in your code where you make TMDB API calls:
- If you're using a custom API wrapper, confirm the
baseURLis set tohttps://api.themoviedb.org/3/(no trailing typos!). - If you're using a third-party TMDB package, make sure you've updated it to the latest version (older versions might hardcode HTTP).
- If you're using a custom API wrapper, confirm the
- Remember: Heroku serves your app over HTTPS by default, and modern browsers block HTTP requests from HTTPS pages to protect users—so every external request must use HTTPS.
2. Invalid API Key Error
This is almost always an environment variable issue. Your local setup works because your .env file has the key, but Heroku doesn't have access to that file. Here's how to fix it:
- First, confirm your local environment variable name follows React's rules: it must start with
REACT_APP_(e.g.,REACT_APP_TMDB_API_KEY). React automatically injects variables with this prefix into your app. - Go to your Heroku dashboard:
- Navigate to your app (
boiling-escarpment-83243). - Click the Settings tab.
- Scroll down to Config Vars and click Reveal Config Vars.
- Add a new entry: use the same variable name as your local
.envfile (e.g.,REACT_APP_TMDB_API_KEY) and paste your TMDB API key as the value.
- Navigate to your app (
- In your code, make sure you're accessing the key via
process.env.REACT_APP_TMDB_API_KEY(not hardcoding it—hardcoding is a security risk anyway!). - Restart your Heroku app to ensure the new environment variable takes effect (you can do this via the dashboard or run
heroku restartin your terminal if you have the Heroku CLI installed). - Double-check that your
.envfile is listed in.gitignore—you never want to commit sensitive keys to Git!
Quick Post-Fix Check
After making these changes:
- Push your updated code to Heroku if you made any code changes (like fixing the base URI).
- Clear your browser cache and reload the app to make sure old assets aren't causing issues.
- Check the DevTools Network tab again to confirm all API requests are using HTTPS and the API key is being passed correctly.
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

