This is my first bigger project creating a job ad database using Flask, flask-restful.
I tried to create a "post" in postman but it doesn't work. I receive this code in python "run":
{'title': 'Internship Cloud Developer', 'describtion': 'Internship for 6 months full-time as Cloud Developer', 'salary': 65000}
127.0.0.1 - - [30/Dec/2022 23:56:06] "POST /jobs HTTP/1.1" 500 -
Traceback (most recent call last):
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\app.py", line 2548, in __call__
return self.wsgi_app(environ, start_response)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\app.py", line 2528, in wsgi_app
response = self.handle_exception(e)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
return original_handler(e)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
return original_handler(e)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask_restful\__init__.py", line 467, in wrapper
resp = resource(*args, **kwargs)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask\views.py", line 107, in view
return current_app.ensure_sync(self.dispatch_request)(**kwargs)
File "F:\Program Files (x86)\PythonProject\Flask\venv\lib\site-packages\flask_restful\__init__.py", line 582, in dispatch_request
resp = meth(*args, **kwargs)
File "F:\Program Files (x86)\PythonProject\Flask\resources\job.py", line 25, in post
description=data['description'],
KeyError: 'description'
The get-method worked without any issues.
I looked at the code and the solutions as well but I havent found any difference. I also followed the instructions to install the versions twice but I did the same as in the solutions.
Thank you for your help. Here is the code:
models\job.py:
job_list = []
def get_last_id():
last_job = 1
if job_list:
last_job = job_list[-1].id 1
return last_job
class Job:
def __init__(self, title, description, salary):
self.id = get_last_id()
self.title = title
self.description = description
self.salary = salary
@property
def data(self):
return {
"id": self.id,
"title": self.title,
"description": self.description,
"salary": self.salary
}
resources\job.py:
from flask import request
from flask_restful import Resource
from http import HTTPStatus
from models.job import Job, job_list
class JobListResource(Resource):
def get(self):
data = []
for job in job_list:
if job.is_published is True:
data.append(job.data)
return {'data': data}, HTTPStatus.OK
def post(self):
data = request.get_json()
print(data)
job = Job(
title=data['title'],
description=data['description'],
salary=data['salary']
)
job_list.append(job)
#job.data = property from models
return job.data, HTTPStatus.CREATED
app-work.py:
from flask import Flask
from flask_restful import Api
from resources.job import JobListResource
app = Flask(__name__)
api = Api(app)
api.add_resource(JobListResource, "/jobs")
if __name__ == "__main__":
app.run(port=5000, debug=True)
CodePudding user response:
{
'title': 'Internship Cloud Developer',
'describtion': 'Internship for 6 months full-time as Cloud Developer',
'salary': 65000
}
This is the problem. "description" is misspelled as "describtion", so when the code tries to access data['description']
, that key is not found.