nisargdave1993
nisargdave1993

Reputation: 113

Unable to update couchbase document using POST REST API

I want to update a couchbase document with REST API

ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}

When I hit below CURL command in postman, I receive 200 OK response Code with response as blank json Array: []

CURL:

curl --location --request POST 'ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9yff' \
--data-raw '{"name": "Nisarg", "age": 50}'

When I retrieve this document by below CURL:

curl --location --request GET 'ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}' \
--header 'Accept: application/json' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9yff'

it responds:

{
    "meta": {
        "id": "112176152456",
        "rev": "4-1637ac65ed7900000000000002000006",
        "att_reason": "invalid_json",
        "expiration": 0,
        "flags": 33554438
    },
    "base64": "",
    "xattrs": {}
}

On Couchbase web console I see message:

"Binary document. Base64 not available"

Can any one please help, what I am doing wrong ?

Upvotes: 1

Views: 640

Answers (1)

Matthew Groves
Matthew Groves

Reputation: 26169

The trick here is that this API doesn't actually accept JSON. It's looking for application/x-www-form-urlencoded. Otherwise it will assume you are storing a binary document. You actually need a form value, which itself contains JSON. For example:

curl --location --request POST 'http://localhost:8091/pools/default/buckets/demo/docs/doc1' \
--header 'Accept: application/json, text/plain, */*' \
--header 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==' \
--data-urlencode 'value={"foo": "bar"}'

And just to echo my comment, this is an undocumented, unsupported endpoint that's really meant for internal use only. It's recommended instead to use an SDK (like the Couchbase Java SDK, for instance) to read/write documents. The REST API is intended for Cluster management, not for CRUD.

Upvotes: 2

Related Questions