简体   繁体   English

Flask-Restful 错误:请求 Content-Type 不是 'application/json'。"}

[英]Flask-Restful Error: request Content-Type was not 'application/json'."}

I was following this tutorial and it was going pretty well.我正在关注本教程,并且进展顺利。 He then introduced reqparse and I followed along.然后他介绍了reqparse ,我也跟着介绍了。 I tried to test my code and I get this error {'message': "Did not attempt to load JSON data because the request Content-Type was not 'application/json'."}我尝试测试我的代码,但收到此错误{'message': "Did not attempt to load JSON data because the request Content-Type was not 'application/json'."}

I don't know if I'm missing something super obvious but I'm pretty sure I copied his code exactly.我不知道我是否遗漏了一些非常明显的东西,但我很确定我完全复制了他的代码。 here's the code: main.py这是代码: main.py

from flask import Flask, request
from flask_restful import Api, Resource, reqparse

app = Flask(__name__)
api = Api(app)

#basic get and post
names = {"sai": {"age": 19, "gender": "male"},
            "bill": {"age": 23, "gender": "male"}}
class HelloWorld(Resource):
    def get(self, name, numb):
        return names[name]

    def post(self):
        return {"data": "Posted"}

api.add_resource(HelloWorld, "/helloworld/<string:name>/<int:numb>")

# getting larger data
pictures = {}
class picture(Resource):
    def get(self, picture_id):
        return pictures[picture_id]

    def put(self, picture_id):
        print(request.form['likes'])
        pass

api.add_resource(picture, "/picture/<int:picture_id>")

# reqparse
video_put_args = reqparse.RequestParser() # make new request parser object to make sure it fits the correct guidelines
video_put_args.add_argument("name", type=str, help="Name of the video")
video_put_args.add_argument("views", type=int, help="Views on the video")
video_put_args.add_argument("likes", type=int, help="Likes on the video")

videos = {}

class Video(Resource):
    def get(self, video_id):
        return videos[video_id]

    def post(self, video_id):
        args = video_put_args.parse_args()
        print(request.form['likes'])
        return {video_id: args}

api.add_resource(Video, "/video/<int:video_id>")

if __name__ == "__main__":
    app.run(debug=True)

test_rest.py test_rest.py

import requests

BASE = "http://127.0.0.1:5000/"

response = requests.post(BASE + 'video/1', {"likes": 10})

print(response.json())

I don't know why you have an issue as far as I can tell you did copy him exactly how he did it.我不知道你为什么有问题,据我所知,你确实复制了他的做法。 Here's a fix that'll work although I can't explain why his code works and yours doesn't.这是一个可行的修复程序,尽管我无法解释为什么他的代码有效而您的代码无效。 His video is two years old so it could be deprecated behaviour.他的视频已有两年历史,因此可能是不推荐使用的行为。

import requests
import json

BASE = "http://127.0.0.1:5000/"

payload = {"likes": 10}

headers = {'accept': 'application/json'}
response = requests.post(BASE + 'video/1', json=payload)

print(response.json())

You can set the header like the error message says.您可以像错误消息中所说的那样设置标题。

import requests, json

BASE = "http://127.0.0.1:5000/"
# Set request's header.
headers = {"Content-Type": "application/json; charset=utf-8"}
# Set data.
data = {"likes": 10}
# 
response = requests.post(BASE + 'video/1', headers=headers, json=data)

print("Status Code: ", response.status_code)
print("JSON Response: ", response.json())

Currently following the same tutorial and faced the same issue.目前遵循相同的教程并面临相同的问题。 Solved mine by adding the keyword argument json for the data通过为数据添加关键字参数json解决了我的问题

response = requests.post(BASE + 'video/1', json={"likes": 10})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM