Home > Software design >  Flask app request.get_json() return (Ellipsis, Ellipsis) instead of json
Flask app request.get_json() return (Ellipsis, Ellipsis) instead of json

Time:12-12

I am building a simple Flask API and I Am testing a post request from Postman, like this {"name": "Frosty"} . This is my class that handles the requests to the endpoint where the POST request goes:

from http import HTTPStatus
from flask.views import MethodView
from flask import Blueprint
from injector import singleton, inject
from flask import jsonify, abort, request
    
    
@singleton
class PetsController(MethodView):
    @inject
    def __init__(self) -> None:
        super().__init__()
        self.pets = [
            {"id": 1, "name": "Snoopy"},
            {"id": 2, "name": "Furball"},
            {"id": 3, "name": "Alaska"},
        ]

def configure(self):
    self.pets_view = Blueprint("pets_view", __name__)
    self.pets_view.add_url_rule("/pets/", view_func=PetsController.as_view("pets"))

def get(self):
    return jsonify({"pets": self.pets})

def post(self):
    data = request.get_json()
    if not data or not "name" in data:
        return jsonify(
            message=f"Data missing from POST request {data}",
            status=HTTPStatus.BAD_REQUEST.value,
        )

    new_pet = {"id": len(self.pets)   1, "name": data["name"]}
    self.pets.append(new_pet)
    return jsonify(
        message=f"new pet added: {new_pet}",
        status=HTTPStatus.CREATED.value,
    )

I am getting a bad request response because the request.get_json() ad request.json both return this tuple (Ellipsis, Ellipsis).

Any ideas why?

CodePudding user response:

Ok it seems that was a Postman glitch, even though I had define in the Headers the Content-Type: application/json it was not sending that information.

I deleted and started a new POST request and first manually added the header and then added the raw body data, selected type JSON and it worked.

  • Related