Did not attempt to load JSON data because the request Content-Type was not 'application/json

53 views Asked by At

I need to receive a GPT chat request from the client, access the DatabaseData and send the formatted response to the user

I've tried everything I can and I don't know what to do now, please help me.

from flask import Flask, jsonify, request
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)


class Main(Resource):
    def get(self):
        from requestDB import moviedata

        movie_data_list = moviedata()
        result = {"movies": movie_data_list}

        return jsonify(result)

    def post(self):
        data_from_client = request.get_json()
        return jsonify(data_from_client)


api.add_resource(Main, "/movie_gpt_ai/back/moviedata")


def get_movie_data():
    client_data = Main().post()
    return client_data


if __name__ == "__main__":
    app.run(debug=True, port=1000, host='localhost')
import requests

url = requests.post("http://localhost:1000/movie_gpt_ai/back/moviedata",
                    json={"name": "Три лучших фильма с Джонни Депом"})
print(url.json())

res = requests.get("http://localhost:1000/movie_gpt_ai/back/moviedata")
print(res.json())
1

There are 1 answers

5
Jim Wright On

The error you mentioned means that you need to specify the Content-Type header.

headers = {'Content-type': 'application/json'}
res = requests.post("http://localhost:1000/movie_gpt_ai/back/moviedata", json={"name": "Три лучших фильма с Джонни Депом"}, headers=headers)