【发布时间】:2023-03-08 01:45:01
【问题描述】:
我有一个简单的烧瓶应用程序
@app.route("/endpoint/", methods=['POST'])
def mypostmethod():
if 'Content-Type' in request.headers and request.headers['Content-Type'] == 'application/json':
post_data = request.json
req_data = request.get_json()
content = req_data['content']
return content
我卷曲如下:
curl -X POST "localhost:8080/endpoint/" -H "Content-Type: application/json" -d '{'content': 'Blah'}'
我收到以下错误:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</p>
这是因为,cURL 中的 json 正文使用单引号,当我使用双引号时它可以工作。
我想知道,如何在我的烧瓶应用程序中添加一些代码来检查此错误并返回自定义响应代码和一些响应消息?在哪里添加 try 块?能否请您以我的代码为例。
【问题讨论】: