Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

postman - KeyError when making post request with form data

I'm trying to make a POST request to create a comment on a user post. I believe I've done everything correctly, so this is likely a silly error, but for some reason I'm getting a TypeError for each of the values I'm trying to grab from my form data.

Here's my POST route for comments:

@comment_routes.route('<int:id>/new', methods=["POST"])
def new_comment(id):
    form = CommentForm()
    post = User_Post.query.get(id)
    if form.validate_on_submit():
        comment = Comment(
            post_id=id,
            user_id=form.data['user_id'],
            content=form.data['content']
        )
        db.session.add(comment)
        db.session.commit()
        return comment.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}

and my FlaskForm:

from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired
from app.models import User


class CommentForm(FlaskForm):
    user_id: IntegerField('user_id', validators=[DataRequired()])
    post_id: IntegerField('post_id', validators=[DataRequired()])
    content: StringField('content', validators=[DataRequired()])

I'm making the POST call in postman with json. This way worked with my post_route, but for some reason I'm getting a typeerror with my comment_route.

the error:

KeyError: 'user_id' // Werkzeug Debugger

I believe it's an issue with the way my form data is parsed from my post request on postman. I'm sending the data as json, but this hasn't caused me issues in the past. Just to be safe, I did switch content-type to form-data and passed it in that way, but still the same error. Here's the pseudocode of what I'm doing on postman:

POST http://localhost:3000/api/comments/1/new 
headers: {
    Content-Type: application/json,
    X-CSRFToken: (....)
}
body: {
    "user_id": 1,
    "content": "testing"
}

and my comment model for reference:

class Comment(Base):
    __tablename__ = "comments"
    query = Session.query_property()

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(1000), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('users_posts.id'))

    def to_dict(self):
        return {
            'id': self.id,
            'content': self.content,
            'user_id': self.user_id,
            'post_id': self.post_id,
        }

    def to_dict_full(self):
        return {
            'id': self.id,
            'content': self.content,
            'user_id': self.user_id,
            'post_id': self.post_id,
            'liked_by': [user.to_dict() for user in self.liked_by]
        }
question from:https://stackoverflow.com/questions/65623348/keyerror-when-making-post-request-with-form-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...