如何成功调用此端点?

How can I successfully call this endpoint?

我正在学习 RESTful Flask API (https://www.rahmanfadhil.com/flask-rest-api/) 的教程,它说要向某物发送请求。我尝试向 Postman 发出请求,但它说 Content-Type 不是 application/json。密码是

from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_restful import Api, Resource

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
ma = Marshmallow(app)
api = Api(app)


# classes
class Post(db.Model):
    post_id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    content = db.Column(db.String(255))

    def __repr__(self):
        return '<Post %s>' % self.title


class PostSchema(ma.Schema):
    class Meta:
        fields = ("post_id", "title", "content")
        model = Post


post_schema = PostSchema()
posts_schema = PostSchema(many=True)


class PostListResource(Resource):
    def get(self):
        posts = Post.query.all()
        return posts_schema.dump(posts)

    def post(self):
        new_post = Post(
            title=request.json['title'],
            content=request.json['content']
        )
        db.session.add(new_post)
        db.session.commit()
        return post_schema.dump(new_post)


api.add_resource(PostListResource, '/posts')


class PostResource(Resource):
    def get(self, post_id):
        post = Post.query.get_or_404(post_id)
        return post_schema.dump(post)

api.add_resource(PostResource, '/posts/<int:post_id>')

教程说 运行

$ curl http://localhost:5000/posts \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"title":"Post 1", "content":"Lorem ipsum"}'

这是我在 Powershell 中所做的,但是 returns 是一个错误:

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type       "System.String" to type "System.Collections.IDictionary".                                                                    At line:1 char:45                                                                                                            + ... alhost:5000/posts -X POST -H "Content-Type: application/json" -d '{"t ...                                              +                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                              + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException                                + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand 

你能帮忙吗?谢谢!

另外,我想知道如何从另一个程序中调用它。我在请求中输入什么 API?

这应该可以,但我没有可用的 POST 端点,所以我无法测试:

$Body = @{
    Title= "Post 1"
    Content = "Lorem ipsum"
}

Invoke-RestMethod -Uri "http://localhost:5000/posts" -Method Post -Body ($Body | ConvertTo-Json)

Postman 在 Headers 中有一个内容类型字段,确保将其设置为 JSON

Content-Type application/json

您也可以在“正文”选项卡中进行设置,但如果不匹配,Postman 会通知您。确保你 select “原始”并将以下内容粘贴到下面的 window 中:

{
    "title":"Post 1", 
    "content":"Lorem ipsum"
}