Plumber @post example not behaving as documented using curl

In section 3.6.3 Request Body of the Plumber docs there is a small example that doesn't work for me. In the bigger project I'm working on I have the same problem that whittles down to the same thing.

#' @post /user
function(req, id, name){
    id = id,
    name = name,
    raw = req$postBody

The curl command

curl --data "id=123&name=Jennifer" "http://localhost:8000/user"

correctly returns this


but the JSON formatted alternative curl as given in the docs produces an error:

curl --data '{"id":123, "name": "Jennifer"}' "http://localhost:8000/user"

curl: (6) Could not resolve host: name
curl: (3) [globbing] unmatched close brace/bracket in column 9
{"error":["500 - Internal server error"]}

I've tried as many variants as I can find on stack overflow and in the curl docs but I can't solve it.

Interestingly, httr produces the correct result:

r <- POST("http://localhost:8000/user", 
    body = list(id=123, name="Jennifer"), 
    encode = "json")
> content(r)
[1] 123

[1] "Jennifer"

[1] "{\"id\":123,\"name\":\"Jennifer\"}"

So maybe I just don't understand curl. Any help appreciated.

Personally I'd be quite happy to proceed with httr but the intention is to run a Docker container with other devs making requests, so ideally I need to get curl working.

Solved my own problem, as seen in the raw output from the httr example above.

curl --data "{\"id\":123, \"name\": \"Jennifer\"}" "http://localhost:8000/user"

In a previous attempt I had outer single quotes with escaped inner double quotes, which doesn't work. Though I still don't see why the example doesn't work as documented with outer single quotes and inner (non-escaped) double quotes.

Now, how to programmatically do the correct escaping with toJSON?

This curl command looks correct to me and works as-is in my environment. If you want to be more explicit with some necessary parameters, you might try:

curl -H "Content-Type: application/json" --data '{"id":123, "name": "Jennifer"}' -X POST "http://localhost:8000/user"

What OS / version of curl are you using? I don't know how to reproduce the error you are getting... honestly Could not resolve host: name and unmatched close brace/bracket in column 9 both sound like issues forming your request: i.e. resolving localhost or interpreting your --data as JSON (hence the application/json header I suggested above).

What do you mean by this? Usually toJSON does the right thing. The only parameter I usually need to alter is auto_unbox, depending on the situation.