Wilson Busaka
Wilson Busaka

Reputation: 21

Error while building RESTful API in Flask

I am learning to build a RESTful API and I am using curl on the command line.

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

It is showing me this error:

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>

Upvotes: 0

Views: 317

Answers (3)

Wilson Busaka
Wilson Busaka

Reputation: 21

So I ran the following and it worked

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

Upvotes: 1

Wilson Busaka
Wilson Busaka

Reputation: 21

@blacksilver this is the code after I ran -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: \

Upvotes: 0

user10452693
user10452693

Reputation:

This is not how you escape JSON on the command line. Use single quotes around the JSON string:

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

If you must use double quotes around the JSON string, you have to escape the double quotes inside it:

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

Upvotes: 1

Related Questions