【问题标题】:Error while building RESTful API in Flask在 Flask 中构建 RESTful API 时出错
【发布时间】:2018-10-11 13:24:06
【问题描述】:

我正在学习构建一个 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>

【问题讨论】:

标签: python api flask


【解决方案1】:

这不是您在命令行中转义 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\"}"

【讨论】:

    【解决方案2】:

    所以我运行了以下,它工作了

    curl -X POST -H "Content-Type: application/json" -d "{ \"title\": \"Read a Book\" }" http://localhost:5000/todo/api/v1.0/tasks

    【讨论】:

      【解决方案3】:

      @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: \
      

      【讨论】:

        猜你喜欢
        • 2015-12-19
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多