简体   繁体   English

documentDB Rest API-创建文档

[英]documentDB Rest API - create document

I'm trying to add documentdb support to an existing app written in dotnetcore, so I can't use the SDK, but thought I'd just use the REST API. 我试图将documentdb支持添加到以dotnetcore编写的现有应用程序中,因此我无法使用SDK,但以为我只会使用REST API。 Easy enough and the samples were easy to duplicate. 足够容易,样品也容易复制。 I can do all the list/queries that are in the REST API sample just fine. 我可以很好地完成REST API示例中的所有列表/查询。

The problem comes when trying to create documents. 尝试创建文档时出现问题。 I am continually faced with a 401 and can't seem to get around it. 我一直面对着401,似乎无法绕开它。 I'm using the primary key - not the read only key. 我正在使用主键-而不是只读键。 And I've read and re-read the API doc here: https://msdn.microsoft.com/en-us/library/azure/mt489088.aspx but can't quite make it work. 而且我已经在这里阅读并重新阅读了API文档: https : //msdn.microsoft.com/zh-cn/library/azure/mt489088.aspx,但不能完全使其正常工作。

It's probably with my auth key, but I'm using the method in the samples GenerateMasterKeyAuthorizationSignature(string verb, string resourceId, string resourceType, string key, string keyType, string tokenVersion) and that works great for the queries. 这可能与我的auth密钥有关,但是我正在使用示例GenerateMasterKeyAuthorizationSignature(string verb, string resourceId, string resourceType, string key, string keyType, string tokenVersion) ,该方法非常适合查询。 I'm using ID based resourceId and thought I could reuse the code from before: 我正在使用基于ID的resourceId,并认为我可以重用之前的代码:

resourceLink = string.Format("dbs/{0}/colls/{1}/docs", databaseId, collectionId);
resourceId = (idBased) ? string.Format("dbs/{0}/colls/{1}/docs", databaseId, collectionId) : collectionId.ToLowerInvariant();`

The timestamp seems right because the queries work, although I've seen that problem stated elsewhere. 时间戳似乎正确,因为查询有效,尽管我已经在其他地方看到了该问题。 And I'm using Ryan's PostWithNoCharSetAsync 我正在使用Ryan的PostWithNoCharSetAsync

I've tried with and without client.DefaultRequestHeaders.Add("x-ms-documentdb-is-upsert", "true"); 我已经尝试了有和没有client.DefaultRequestHeaders.Add("x-ms-documentdb-is-upsert", "true");

Could there be something with partitions? 分区可能有东西吗? Should I be specifying that? 我应该指定吗?

The API docs have headers like cookies and no-cache. API文档具有标头,例如cookie和no-cache。 Those aren't making a difference are they? 那些没有改变,是吗?

Does it matter if the JSON document comes across with string literal markings - like this: " {\\r\\n \\"id\\": 4441,\\r\\n \\"Name\\": \\"Artesia Spa - Grand Hotel\\",\\r\\n } JSON文档是否带有字符串文字标记是否重要-像这样: " {\\r\\n \\"id\\": 4441,\\r\\n \\"Name\\": \\"Artesia Spa - Grand Hotel\\",\\r\\n }

Below is the POST from fiddler. 下面是提琴手的帖子。

REQUEST 请求

POST https://etest.documents.azure.com/dbs/etest/colls/unittest/docs HTTP/1.1
x-ms-date: Tue, 08 Nov 2016 20:34:40 GMT
x-ms-version: 2015-12-16
authorization: type%3dmaster%26ver%3d1.0%26sig%3dU8BmnPhUMWyoVqNdbI0hy1Kc%2b1Yge79dCBMz8f2v9pE%3d
x-ms-documentdb-is-upsert: true
Content-Type: application/query+json
Host: etest.documents.azure.com
Content-Length: 48
Expect: 100-continue

{"id": 4441,"Name": "Artesia Spa - Grand Hotel"}

RESPONSE 响应

HTTP/1.1 401 Unauthorized
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
x-ms-activity-id: d83ae44f-3dc8-47a6-b310-cdf8ca87c597
Strict-Transport-Security: max-age=31536000
x-ms-gatewayversion: version=1.10.39.1
Date: Tue, 08 Nov 2016 20:36:11 GMT
Content-Length: 358

{"code":"Unauthorized","message":"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'post\ndocs\ndbs/etest/colls/unittest\ntue, 08 nov 2016 20:34:40 gmt\n\n'\r\nActivityId: d83ae44f-3dc8-47a6-b310-cdf8ca87c597"}

Since I just spent like 2 days trying to figure this out, here is an actual answer for anyone interested. 由于我只花了2天的时间来解决这个问题,因此这是有兴趣的人的实际答案。 DocumentDB is now CosmosDB currently. 现在,DocumentDB现在是CosmosDB。

Create a DB 创建一个数据库

var verb = "POST";
var resourceId = "";
var resourceType = "dbs";
var resourceLink = "dbs"; 
var body = new { id = "<databaseId>" };

Create a Collection 创建收藏

var verb = "POST";
var resourceId = "dbs/<databaseId>";
var resourceType = "colls";
var resourceLink = "dbs/<databaseId>/colls"; 
var body = new { id = "<collectionId>" };

Create a Document 建立文件

var verb = "POST";
var resourceId = "dbs/<databaseId>/colls/<collectionId>";
var resourceType = "docs";
var resourceLink = "dbs/<databaseId>/colls/<collectionId>/docs"; 
var body = new { id = "<documentId>" };

Create a User 创建一个用户

var verb = "POST";
var resourceId = "dbs/<databaseId>";
var resourceType = "users";
var resourceLink = "dbs/<databaseId>/users"; 
var body = new { id = "<userId>" };

**Basically resourceId is the same as resourceLink but without the last option (which was not obvious to me) **基本上resourceId与resourceLink相同,但没有最后一个选项(这对我来说并不明显)

Setup request something like this ( see the example program from msft for the rest ) 安装程序要求类似这样的内容(有关其他信息, 请参见msft的示例程序

var client = new System.Net.Http.HttpClient();
string response = string.Empty;
string authHeader = string.Empty;

authHeader = GenerateMasterKeyAuthorizationSignature(verb, resourceId, resourceType, key, keyType, tokenVersion);

client.DefaultRequestHeaders.Add("x-ms-date", utc_date);
client.DefaultRequestHeaders.Add("x-ms-version", "2017-02-22");
client.DefaultRequestHeaders.Remove("authorization");
client.DefaultRequestHeaders.Add("authorization", authHeader);

Also note that the sample program from MSFT includes a NoCharSetJsonMediaTypeFormatter for querying. 还要注意,来自MSFT的示例程序包括一个用于查询的NoCharSetJsonMediaTypeFormatter。 This needs to be updated for POSTs since the header needs to be "application/json" instead of "application/query+json" 对于POST,需要对此进行更新,因为标头需要为“ application / json”而不是“ application / query + json”

There is now an official .Net Core SDK you can obtain and use, just like the previous SDK, from Nuget . 现在有一个正式的.Net Core SDK,您可以像以前的SDK一样从Nuget获取和使用。

In case you need a full sample you can take a look at a GitHub repo I published which covers the most common scenarios. 如果您需要完整的样本,可以看一下我发布的GitHub存储库 ,其中涵盖了最常见的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM