Python 在Flask中构建RESTful API时出错

Python 在Flask中构建RESTful API时出错,python,api,flask,Python,Api,Flask,我正在学习构建RESTful API,并在命令行上使用curl curl -i -H "Content-Type:application/json" -X POST \ -d "{"""title""":"""Read a book"""}" \ http://localhost:5000/todo/api/v1.0/tasks 它向我显示了以下错误: curl: (6) Could not resolve host: a curl: (3) [globbing] unmatched

我正在学习构建RESTful API,并在命令行上使用curl

curl -i -H "Content-Type:application/json" -X POST \
  -d "{"""title""":"""Read a book"""}" \
  http://localhost:5000/todo/api/v1.0/tasks
它向我显示了以下错误:

curl: (6) Could not resolve host: a
curl: (3) [globbing] unmatched close brace/bracket in column 6
HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 204
Server: Werkzeug/0.14.1 Python/3.7.0
Date: Thu, 11 Oct 2018 13:20:28 GMT

<!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: Unterminated string starting at: line 1 column 10 (char 9)</p>
curl:(6)无法解析主机:a
卷曲:(3)[globbing]第6列中不匹配的右括号/括号
HTTP/1.0 400错误请求
内容类型:text/html
内容长度:204
服务器:Werkzeug/0.14.1 Python/3.7.0
日期:2018年10月11日星期四格林威治标准时间13:20:28
400错误请求
错误的请求
无法解码JSON对象:从第1行第10列(字符9)开始的未终止字符串


这不是在命令行上转义JSON的方式。在JSON字符串周围使用单引号:

curl -X POST "https://httpbin.org/post" -H "accept: application/json"
  -H "Content-Type: application/json" \
  -d '{"foo": "bar"}'
如果必须在JSON字符串周围使用双引号,则必须转义其中的双引号:

curl -X POST "https://httpbin.org/post" -H "accept: application/json" \
  -H "Content-Type: application/json" \
  -d "{\"foo\": \"bar\"}"

这不是在命令行上转义JSON的方式。在JSON字符串周围使用单引号:

curl -X POST "https://httpbin.org/post" -H "accept: application/json"
  -H "Content-Type: application/json" \
  -d '{"foo": "bar"}'
如果必须在JSON字符串周围使用双引号,则必须转义其中的双引号:

curl -X POST "https://httpbin.org/post" -H "accept: application/json" \
  -H "Content-Type: application/json" \
  -d "{\"foo\": \"bar\"}"

@blacksilver这是我运行-v后的代码

Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> POST /todo/api/v1.0/tasks HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.61.0
> accept: application/json
>
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Content-Type: text/html
< Content-Length: 192
< Server: Werkzeug/0.14.1 Python/3.7.0
< Date: Thu, 11 Oct 2018 13:56:11 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
* Closing connection 0
* Rebuilt URL to: \/
* Could not resolve host: \
* Closing connection 1
curl: (6) Could not resolve host: \
正在尝试::1。。。
*TCP_节点集
*正在尝试127.0.0.1。。。
*TCP_节点集
*已连接到本地主机(127.0.0.1)端口5000(#0)
>POST/todo/api/v1.0/tasks HTTP/1.1
>主机:本地主机:5000
>用户代理:curl/7.61.0
>接受:application/json
>
*HTTP 1.0,假设在正文之后关闭

*正在关闭连接0
*重新生成的URL到:\/
*无法解析主机:\
*关闭连接1
curl:(6)无法解析主机:\

@blacksilver这是我运行-v之后的代码

Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> POST /todo/api/v1.0/tasks HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.61.0
> accept: application/json
>
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Content-Type: text/html
< Content-Length: 192
< Server: Werkzeug/0.14.1 Python/3.7.0
< Date: Thu, 11 Oct 2018 13:56:11 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
* Closing connection 0
* Rebuilt URL to: \/
* Could not resolve host: \
* Closing connection 1
curl: (6) Could not resolve host: \
正在尝试::1。。。
*TCP_节点集
*正在尝试127.0.0.1。。。
*TCP_节点集
*已连接到本地主机(127.0.0.1)端口5000(#0)
>POST/todo/api/v1.0/tasks HTTP/1.1
>主机:本地主机:5000
>用户代理:curl/7.61.0
>接受:application/json
>
*HTTP 1.0,假设在正文之后关闭

*正在关闭连接0
*重新生成的URL到:\/
*无法解析主机:\
*关闭连接1
curl:(6)无法解析主机:\

所以我运行了下面的程序,它成功了


curl-X POST-H“Content-Type:application/json”-d“{\”title\:\”Read a Book\“}”

因此,我运行了以下程序,它成功了


curl-X POST-H“Content-Type:application/json”-d“{\”title\:\”Read a Book\“}”

可能与json中的引号有问题。看一看这里的例子,也许它会奏效。你能用
-v
标志再次运行相同的命令并发布输出吗?JSON中的引号可能有问题。看一看这里的例子,也许它会奏效。你能用
-v
标志再次运行相同的命令并发布输出吗?