سامان صفایی
4 سال پیش توسط سامان صفایی مطرح شد
0 پاسخ

سوال در مورد Response headers و json

سلام دوستان . من توی فلسک یه api ساختم خروجیش باید فارسی باشه ولی اینجوریه

{"data":[{"description":"\u062f\u0627\u0631\u0627\u06cc \u06a9\u0627\u0641\u0626\u06cc\u0646 \u0628\u0627\u0644\u0627 . \u0628\u0627 \u0637\u0639\u0645\u06cc \u0645\u0644\u0627\u06cc\u0645","discount":0,"id":1,"image_url":"https://api-coffee-flask.herokuapp.com/uploads/202111271747-coffee1.jpeg","name":"\u0642\u0647\u0648\u0647 \u062a\u0631\u06a9\u06cc\u0628\u06cc","off_price":0,"price":40000}]}

ولی میخوام اینجوری باشه

{
  "data": [
    {
      "description": "دارای کافئین بالا . با طعمی ملایم",
      "discount": 0,
      "id": 1,
      "image_url": "https://api-coffee-flask.herokuapp.com/uploads/202111271747-coffee1.jpeg",
      "name": "قهوه ترکیبی",
      "off_price": 0,
      "price": 40000
    }
  ]
}

مرتب بودنش مهم نیس
اون متن هاش مهمه که هیچجوره نتونستم فارسی کنمش
چیکارش کنم ؟
کانتنت تایپش اینه :
application/json; charset=utf-8

اینم کد های قلسک من

    def get(self):
        db.create_all()
        product_id = request.args.get("id", None)
        response_list = list()
        if not product_id:
            all_data = models.MProducts.query.all()
            for data in all_data:
                response_list.append({
                    "id": data.get_id(),
                    "name": data.name,
                    "image_url": data.image_url,
                    "description": data.description,
                    "price": data.price,
                    "discount": data.discount,
                    "off_price": data.off_price
                })
        else:
            single_data = models.MProducts.query.filter_by(_id=product_id).first_or_404()
            response_list.append({
                "id": single_data.get_id(),
                "name": single_data.name,
                "image_url": single_data.image_url,
                "description": single_data.description,
                "price": single_data.price,
                "discount": single_data.discount,
                "off_price": single_data.off_price
            })
        response = make_response(dict(data=response_list))
        response.content_type = "application/json; charset=utf-8"
        response.charset = "UTF-8"
        response.content_language = "fa-IR"
        return response