1

I am on a Windows platform and I have a file called agent_import.json in a c:\Temp directory.

The file contains the following test JSON document(s) which to my knowledge is valid JSON:

{
    docs: [{
        "_id": "11",
        "name11": "test11"
    }, {
        "_id": "12",
        "name12": "test12"
    }]
}

I have a CouchDB installation and test database called agents installed on Windows.

From the cURL command line I execute:

curl -vX POST -H "Content-Type":"application/json" -d @c:\Temp\agent_import.json http://127.0.0.1:5984/agents/_bulk_docs

The above curl command fails, complaining about the JSON.

*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5984 (#0)
> POST /agents/_bulk_docs HTTP/1.1
> Host: 127.0.0.1:5984
> User-Agent: curl/7.48.0
> Accept: */*
> Content-Type:application/json
> Content-Length: 25
>
* upload completely sent off: 25 out of 25 bytes
< HTTP/1.1 400 Bad Request
< Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
< Date: Wed, 27 Apr 2016 16:42:51 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 48
< Cache-Control: must-revalidate
<
{"error":"bad_request","reason":"invalid_json"}
* Connection #0 to host 127.0.0.1 left intact

I have tried changing the content of the Json file so that every double quote is proceeded with a backslash as listed below but alas the same error as above...

{
    docs: [{
        \"_id\": \"11\",
        \"name11\": \"test11\"
    }, {
        \"_id\": \"12\",
        \"name12\": \"test12\"
    }]
}

I have seen people post similar issues but not exactly the same so I wanted to ask if I am doing something obviously wrong here with regards to the use of my curl command and/or my expectations of _bulk_docs.

Regards

2 Answers 2

1

First of all the json you are trying to post is invalid - docs should be in quotes. The correct one is:

{
  "docs": [{
    "_id": "11",
    "name11": "test11"
  }, {
    "_id": "12",
    "name12": "test12"
  }]
}

You can always validate the file with tools like jq. I've just tried with he updated file and curl 7.48.0 on Windows and it did work. Alternatively you could try and use the option --data-binary

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gytis, you are absolutely right. Changing the docs to "docs" worked perfectly! simple as that! Many thanks
0

If you take a look in your curl headers you can see the first clue:

Content-Length: 25

It should be clear to you that no amount of fiddling will get your JSON content down to 25 bytes.

On the other hand, the string: @c:\Temp\agent_import.json is 27 bytes, assume those \s are being treated as escapes and it's precisely 25 bytes.

No idea how your version of curl works, but it would seem like it's not processing the @ based file input to -d like the *nix version does.

2 Comments

Just to add, the version of curl is 7.48.0 which is a recent build so unlikely to be the issue. It is more likely, as you suggest, that I have the wrong directive on the command line as -d is saying what follows is {json}. I now need to find what the command line needs in order to read the supplied file :(
I meant to imply that it was the fact that you were on Windows that's causing this problem, the -d @filename works perfectly in *nix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.