Issue loading JSON documents from file into CouchDB from cURL command line

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:Tempagent_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


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


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:Tempagent_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.

链接地址: http://www.djcxy.com/p/8538.html

上一篇: 如何通过curl调用使用HTTP请求发送头文件?

下一篇: 问题从cURL命令行将JSON文档从文件加载到CouchDB中