skip to Main Content

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 different page i try different methods of applying UTF-8 into new-movie-data but none of them work.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks bro @hc_dev its working the problem is in my MOVIE_DB_SEARCH url and request i put first api key and then query but in your code query is first and in last is api-key.

    HERE is my Movie-DB-search url and response: '

    MOVIE_DB_SEARCH ="https://api.themoviedb.org/3/search/movie?api_key="
     response = requests.get(f"{MOVIE_DB_SEARCH}{api_key}&query={movie_title}")
    

    '


    Also i didnt put headers in request but it also works without putting headers in request ....


  2. 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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search