Home > database >  Create new firestore document with REST API
Create new firestore document with REST API

Time:12-06

I'm trying to create a new document id and store my collection inside it. My post looks like this:

{     
 "name": "projects/<project-id>/databases/(default)/documents/users/cena123",

  "fields": {
    "name": {
      "stringValue": "tom"
    },
    "lname": {
      "stringValue": "cruise"
    }

ERROR i get:

error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \" {\r\n \"name\": \"projects/mountain-bear-****/databases/(default)/documents/users/cena123\",\r\n

What T'm trying to achieve:

  1. Create a document called: "cena123"
  2. Add name and lname fields under the document

CodePudding user response:

The reason you are receiving "code": 400, "message": "Invalid JSON payload received. Unknown name error, is because name should not be passed in the request body, unless it's inside the fields object. Use the below json in your request body.

 {
      "fields": {
        "name": {
          "stringValue": "tom"
        },
        "lname": {
          "stringValue": "cruise"
        }
    }
}
  • Related