Rhys
Rhys

Reputation: 79

How should a "write" request be structured for Firestore REST API (v1beta1)?

Based on the Google Discovery document, and RPC reference, it appears that the :write resource should be available for Firestore database interactions, but performing such a request to my (POST https://firestore.googleapis.com/v1beta1/projects/[my project]/databases/(default)/documents:write) results in:

[
  {
    "error": {
      "code": 400,
      "message": "Invalid value (Object), ",
      "status": "INVALID_ARGUMENT",
      "details": [
        {
          "@type": "type.googleapis.com/google.rpc.BadRequest",
          "fieldViolations": [
            {
              "description": "Invalid value (Object), "
            }
          ]
        }
      ]
    }
  }
]

Is this possible? A related SO answer alludes to this being available as a means of field transforms, the same reason I require this, but I cannot construct valid a JSON body to succeed in the request. Currently, variations on the following don't work as expected when trying a minimum successful response:

{
    "writes": [
        {
            "update": {
                "name": "projects/{projectId}/databases/[my project]/documents/exampleId",
                "fields": {
                    "example": {
                        "integerValue": 100
                    },
                    "timestamp": {
                        "nullValue": null
                    }
                },
                "transform": {
                    "document": "projects/[my project]]/databases/(default)/documents/examples/exampleId",
                    "fieldTransforms": [
                        {
                            "fieldPath": "timestamp",
                            "setToServerValue": "REQUEST_TIME"
                        }
                    ]
                }
            }
        }
    ]
}

Upvotes: 2

Views: 900

Answers (2)

Gerry St.Pierre
Gerry St.Pierre

Reputation: 201

You need to use documents:commit instead of documents:write

also the name field should be in this format:

"name": "projects/projectID/databases/(default)/documents/collectionName/DocumentId"

See this post.

Upvotes: 1

Renaud Tarnec
Renaud Tarnec

Reputation: 83113

First of all, note that you should use the v1 version of the REST API, not the betas.

To create a document, you would use the createDocument method, while to update a document you would use the patch one.


For the document creation you should therefore make a POST HTTP Request to the following URL

https://firestore.googleapis.com/v1/projects/<your-project-id>/databases/(default)/documents/<the-desired-collection>

with the following Request body:

{
  fields: {
    example: {
      integerValue: 100
    }
  }
}

Upvotes: 2

Related Questions