from flask import request
from flask_restful import Resource
from models import db, Infrastructure
class InfrastructureResource(Resource):
def get(self, id=None):
if id:
infra = Infrastructure.query.get_or_404(id)
return {"id": infra.id, "name": infra.name, "location": infra.location}
else:
infra_list = Infrastructure.query.all()
return [{"id": infra.id, "name": infra.name, "location": infra.location} for infra in infra_list]
def post(self):
data = request.get_json()
new_infra = Infrastructure(
name=data['name'],
location=data['location']
)
db.session.add(new_infra)
db.session.commit()
return {"message": "Успешно добавлено", "id": new_infra.id}, 201
class InfrastructureSearchResource(Resource):
def get(self):
name = request.args.get('name')
location = request.args.get('location')
query = Infrastructure.query
if name:
query = query.filter(Infrastructure.name.like(f'%{name}%'))
if location:
query = query.filter(Infrastructure.location.like(f'%{location}%'))
infra_list = query.all()
return [{"id": infra.id, "name": infra.name, "location": infra.location} for infra in infra_list]
POST query - (Invoke-WebRequest -Uri http://127.0.0.1:5000/infrastructure -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"name": "имя", "location": "адрес"}').Content
GET query - (Invoke-WebRequest -Uri http://127.0.0.1:5000/infrastructure).Content
i got
[
{
"id": 1,
"name": "???",
"location": "?????"
},
{
"id": 2,
"name": "???",
"location": "?????"
}
]
how i can fix it?
i try encode and decode args to cp1251 and utf-8, thats not helped for me
also i tried JSON_AS_ASCII = False
in my config file
New contributor
Михаил Гындыбин is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.