在Couchbase中添加文档并缺少JSON正文 [英] Adding document in Couchbase and missing JSON body

查看:92
本文介绍了在Couchbase中添加文档并缺少JSON正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Couchbase REST API将文档添加到现有文档中。

I am trying to use Couchbase REST API to add a document to the existing documents. I am just testing this in Postman while writing on the code.

POST:

http://<ip>:8091/pools/default/buckets/<bucketname>/docs/testdoc1?rev=1

Headers:

Accept: application/json
Authorization : xxxxx

Body:

Raw JSON (application/json)
{
  "Name": "xxx",
  "Address": "yyy",
  "Phone number": "xxxx",
  "Badge": "yyy",
  "BadgeType": "xxx"
} 

当我在邮递员中发送邮件时,它正在添加此新文档。在benchbase文档/存储桶下,但在body字段上显示为 Binary Document,base64不可用。

When I send above in Postman, It is adding this new doc. under couchbase documents/bucket, but on the body field it shows like, "Binary Document, base64 not available"

我甚至尝试了html代码,但json body却没有

I tried even from my html code, but json body didn't receive at couchbase end.

<!DOCTYPE html>
<html>
<body>

<input type="submit" value="Start" onclick="submit()">

<script type="text/javascript">
var params = {
  "Name": "xxx",
  "Address": "yyy",
  "Phone number": "xxxx",
  "Badge": "yyy",
  "BadgeType": "xxx"
} 
    function submit() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                alert(xhr.response);
            }
        }
        xhr.open('post', 'http://<ip>:8091/pools/default/buckets/<buckname>/docs/testdochtml?rev=1', true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.setRequestHeader('Accept', 'application/json');
        xhr.setRequestHeader('Authorization', 'Basic ' + 'xxxxxxx');

        xhr.send(JSON.stringify(params));
    }
</script>

<p>Click on the submit button.</p>

</body>
</html>

有人可以指导我为什么JSON无法以正确的方式进入应用吗?

Can someone guide me why is that JSON not going to couchbase in a correct way?

推荐答案

首先,据我所知,不支持该端点,并且未记录该端点。如果您看到某处支持 ,请告诉我,因为我认为这需要纠正。您应该改用其中一种SDK(Java,.NET,Node等)。

First off: as far as I know, this endpoint is not supported and it is not documented. If you see somewhere that it is supported, let me know, because I think that needs corrected. You should use one of the SDKs (Java, .NET, Node, etc) instead.

话虽这么说,我能够它通过邮递员工作。您不仅可以发送原始JSON作为文档。您需要POST编码的表单数据。该终结点期望的值之一是值,其中包含编码的JSON文档。

That being said, I was able to get it working via Postman. You can't just send raw JSON as the document. You need to POST encoded form data. One of the values this endpoint expects is "value", which contains the encoded JSON document.

这是我所做的一个示例(我将存储桶称为 so) :

Here's an example of what I did (I called my bucket "so"):

POST /pools/default/buckets/so/docs/testdoc2 HTTP/1.1
Host: localhost
cache-control: no-cache
Postman-Token: ba87ef4e-4bba-42b4-84da-ae775b26dbcb
value=%7B%0A%09%22Name%22%3A+%22xxx%22%2C%0A%09%22Address%22%3A+%22yyy%22%2C%0A%09%22Phone+number%22%3A+%22xxxx%22%2C%0A%09%22Badge%22%3A+%22yyy%22%2C%0A%09%22BadgeType%22%3A+%22xxx%22%0A%7D%0A

注意上面的值只是您问题中的URL编码的JSON(邮递员为我编码了它,邮递员也必须自己添加了缓存控制标头,因为我没有指定):

Note that value above is just the URL-encoded JSON from your question (Postman encoded it for me, and Postman must have added the cache-control header on its own too, because I did not specify that):

这篇关于在Couchbase中添加文档并缺少JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆