Issue
Explanation :
When I print new_movie_data it gives Unicode Error:
Error : UnicodeEncodeError: 'charmap' codec can't encode characters in position 4167-4177: character maps to <undefined>
My CODE :
@app.route("/add", methods=["GET", "POST"])
def add():
add_form = FindMovieForm()
if add_form.validate_on_submit():
movie_title = add_form.add_movie.data
response = requests.get(f"{MOVIE_DB_SEARCH}{api_key}&query={movie_title}")
new_movie_data = response.json()["results"]
redirect(url_for("home"))
return render_template("add.html", form=add_form)
I want to print new_movie_data
in JSON so I can parse this data into another template which shows on a different page. I tried different methods of applying UTF-8 into new-movie-data
but none of them work.
Solution
Your request and response seems to be the issue (not Flask).
Since you mentioned "MOVIE_DB_SEARCH" I took this URL and API for my minimal reproducible example:
import requests
# Given those definitions
movie_title = 'Ghost Busters'
MOVIE_DB_SEARCH = 'https://api.themoviedb.org/3/search/movie'
api_key = '' # enter your API key here
bearer_token = '' # enter your Bearer token here
url = f"{MOVIE_DB_SEARCH}?query={movie_title}&api_key={api_key}"
headers_with_bearer = {"accept": "application/json", "Authorization": f"Bearer {bearer_token}"}
# This would be the way to handle request and response
response = requests.get(url, headers=headers_with_bearer)
# Print some debug information (HTTP status, encoding, MIME-type, content)
print(response.status_code) # should be 200
print(response.encoding) # should be utf-8
print(response.headers['Content-Type']) # should be application/json
print(response.content)
new_movie_data = response.json()["results"]
Probably you have more chance when taking the binary-stream from response.content
.
See: python requests.get() returns improperly decoded text instead of UTF-8?
See also
The Movie Database API docs and reference:
Answered By - hc_dev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.