0

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>
  • Might have a problem with the quotes in the JSON. Take a look at the example here, maybe it'll do the trick https://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest – SnoozeTime Oct 11 '18 at 13:31
  • Could you run the same command again with the `-v` flag and post the output? – SIGSTACKFAULT Oct 11 '18 at 13:32

3 Answers3

1

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\"}"
1

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

0

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