Post Json document with Curl


#1

Hi All,
I am trying to insert document with the help of Curl command but facing the following issue.
{
“requestID”: “7531f11a-c597-4fc5-ad48-XXXXXXX”,
“errors”: [{“code”:1065,“msg”:“Unrecognized parameter in request: {“name”:“name”,“Price”:“Price”}”}],
“status”: “fatal”,
“metrics”: {“elapsedTime”: “65.773µs”,“executionTime”: “65.549µs”,“resultCount”: 0,“resultSize”: 0,“errorCount”: 1}
}

Command:
curl -X POST -u ‘Administrator:password’ -H “Content-Type: application/x-www-form-urlencoded” -d @input.json http://localhost:8093/query/service

input.json:
{“name”:“name”,“Price”:“Price”}

I could able to insert with the help of “statement=INSERT INTO …” and cbimport.
But I would like to insert the doc with curl.

Note: Can't create document with POST request – Looks it used for older versions and could not able to access the doc.

Any suggestions/help pls?


#2

Hello,

The following “input.txt” file works for me:

statement=INSERT INTO testLoad ( KEY, VALUE )
  VALUES
  (
    "MyKey_001",
    {"name":"name","Price":"Price"}
  )
RETURNING META().id;

Then run curl like this:

curl -X POST -u ‘Administrator:password’ -d @input.txt http://cb5box:8093/query/service

Notice the following:

  • POST body expected is “statement=…”, rather than plain JSON
  • You need to specify your own bucket name (testLoad in my example)
  • You need to include the document key
  • File extension as .txt, since not really a json doc.

#3

Thanks Manuel.
Do we have any cbimport json equivalent in Curl to load the Json directly instead of providing the statement in the txt file .


#4

Hello,

Yes, we have a cbimport tool to direct load json documents into couchbase. However, it is not a curl interface, but a command line binary included with Couchbase. Documentation here.

cbdocloader is another tool you can explore for inserting json doc files.


#5

You’ve posted under mobile/Sync Gateway, so I’m assuming you have that running. In that case, you can load documents through Sync Gateway as well.

For example

curl -X PUT -H "Content-Type: application/json" -d '{ "Price": "price" }' localhost:4985/db/my_doc_id

Or, if you want to avoid having to read out of a file, the command for Couchbase Server would be

curl localhost:8093/query/service -u Administrator:Password -d 'statement=INSERT INTO `db` (KEY, VALUE) VALUES ( "my_doc_id",  {"Price":"price"} );'

#6

You can also load documents using a N1QL query with the CURL() function. But the data needs to be hosted somewhere (it doesnt allow loading data from files, only the http, https protocols are supported not file:// ).

For eg -
insert into default (key UUID(), VALUE a)
select a
from curl(“https://api.yelp.com/v3/businesses/search?term=Ramen&location=2440%20West%20El%20Camino%20Real%2C%20Mountain%20View%2C%20CA&radius=16093”,
{“header”:“Authorization: Bearer <YOUR_YELP_API_AUTH_HEADER>”}).businesses a;