HTTP: documents and import
Write, read and delete one document by key over HTTP, and load many with NDJSON import, with exact requests and answers from the tests.
A row is a key and a document (a JSON object). Over HTTP the key is a UTF-8 string, percent-encoded in the path. In the binary protocol a key is raw bytes.
Endpoints#
| Request | Meaning |
|---|---|
POST /v1/databases/{db}/tables/{t}/documents body {"key":"...","document":{...}} | insert or replace |
GET /v1/databases/{db}/tables/{t}/documents/{key} | read |
DELETE /v1/databases/{db}/tables/{t}/documents/{key} | delete |
POST /v1/documents, GET/DELETE /v1/documents/{key} | the same for main.documents |
POST .../tables/{t}/import | bulk load, NDJSON |
Write, read, delete#
From documents_can_be_written_read_and_deleted_over_http:
POST /v1/documents
{"key":"user 1/a","document":{"name":"Asha","age":30,"big":9007199254740993}}
-> 200 {"existed":false}
POST /v1/documents
{"key":"user 1/a","document":{"name":"Asha","age":31}}
-> 200 {"existed":true}
GET /v1/documents/user%201%2Fa
-> 200, body has "document" with "age":31
GET /v1/documents/nope -> 404
DELETE /v1/documents/user%201%2Fa
-> 200 {"existed":true}The write is answered after it is durable. A second write to a key replaces the document and answers existed: true.
Bad requests#
| Request | Status |
|---|---|
body without document ({"key":"k"}) | 400 |
document with a duplicate field ({"a":1,"a":2}) | 400 |
key with a bad percent escape (/v1/documents/%zz) | 400 |
| a schema violation | refused; over the binary protocol the code is SCHEMA (the HTTP status is not documented here) |
Both doors see the same data#
A document written over HTTP is found by a binary Client: c.count(r#"{"name":"Asha"}"#) returns 1 and c.get(b"user 1/a") returns the stored document. Query results over HTTP carry each document's key in keys (UTF-8 keys as strings).
Bulk import#
POST /v1/databases/{db}/tables/{t}/import takes an NDJSON body: one {"key":"...","document":{...}} per line. The body is under 1 MiB and at most 20,000 lines. The answer comes when the lines are durable:
{"imported": 3000, "failedCount": 3, "failed": [{"line": 3002, "error": "..."}, ...]}In bulk_import_writes_durably_and_reports_bad_lines the body had 3,000 good lines, a blank line (line 3001, skipped) and three bad ones (not json at all, a line without document, a document with a duplicate field). The answer was imported 3000 and failed lines 3002, 3003, 3004; a binary count("{}") then returned 3000, and after a restart it still did. GET on the import path is 405.
Large integers#
JSON numbers written without . or exponent are stored as integers, others as doubles; 30 and 30.0 stay different types. The document "big":9007199254740993 (beyond 2^53) is stored exactly as an integer. When reading as JSON, parse numbers with a parser that keeps 64-bit integers.
See also#
HTTP: queries, Documents and schemas, Pipelining and batching