Oha*_*rry 5 python flask flask-restful
使用 test_client 并发送如下请求:
app = Flask(__name__)
client = app.test_client()
headers = {'content-type': 'application/json', 'Authorization': u'Bearer fake_token_123'}
params = {'dont_care': True}
client.get(??'/my_route/123', query_string=params, headers=headers)
Run Code Online (Sandbox Code Playgroud)
我的路线是
class MyController(Resource):
def get(self, id):
parser = reqparse.RequestParser()
parser.add_argument('currency', type=str, default='USD', help="help text")
args = parser.parse_args()
currency_from_params = args.currency
Run Code Online (Sandbox Code Playgroud)
parser.parse_args() 失败
The browser (or proxy) sent a request that this server could not understand
Run Code Online (Sandbox Code Playgroud)
'content-type': 'application/json'从标题中删除时,它可以工作。
我不明白这种行为,如果没有 un优雅的try, expect.
谢谢你的帮助
您已经发现了如何修复它:content-type: application/json如果您不发布 JSON,则不要发送。您无法使用 GET 发送 JSON,即使您可以(或正在使用 POST),您也必须json.dumps(data)先对 JSON 进行编码。