Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update single field on Firestore database (cloud) with simple https request

I'm currently using Swift (macOS) & that doesn't matter.

I can get my database data with this simple get request from Firestore

https://firestore.googleapis.com/v1beta1/projects/MY_PROJECT_NAME/databases/(default)/documents/" + "User_email" + "?pageSize=100&pageToken=" + "NextPage_Id"

Now I want to update one single filed on the database & I don't know how.

The Firebase api guide is too complicated for me & I don't understand it.

I found this get request url:

https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/{document_path}?updateMask.fieldPaths=status&updateMask.fieldPaths=title

But when I try it, it gives me this error:

 {
"details" : [
  {
    "fieldViolations" : [
      {
        "description" : "Invalid JSON payload received. Unknown name \"updateMask.fieldPaths\": Cannot bind query parameter. Field 'updateMask' could not be found in request message."
      }
    ],
    "@type" : "type.googleapis.com\/google.rpc.BadRequest"
  }
],
"code" : 400,
"message" : "Invalid JSON payload received. Unknown name \"updateMask.fieldPaths\": Cannot bind query parameter. Field 'updateMask' could not be found in request message.",


  "status" : "INVALID_ARGUMENT"
  }
}

I Use above url like this:

https://firestore.googleapis.com/v1beta1/projects/MY_PROJECT_NAME/databases/(default)/documents/" + User_email + "/MY_COLLECTION_ID/?updateMask.fieldPaths=name&updateMask.fieldPaths=aaaa

Where did I do wrong?

like image 913
Ahmadreza Avatar asked Mar 05 '23 19:03

Ahmadreza


1 Answers

I finally got the answer after getting help from FireBase support.

The request type must be PATCH, not POST or GET or anything else.

The request format must be "application/json"

The Body parameters type must be RAW data not form-data, for example:

["fields": ["finished": ["booleanValue": true]]] // this is the value you want to update

The url must be like this:

https://firestore.googleapis.com/v1beta1/\(VALUE_PATH)/?updateMask.fieldPaths=\(NAME_OF_KEY_YOU_WANT_TO_UPDATE)

for example:

https://firestore.googleapis.com/v1beta1/projects/PROJECT_NAME/databases/(default)/documents/USER_EMAIL/4EJzurX9QWVnC1uG6sRv/?updateMask.fieldPaths=finished

Thats it.

Cool thing about this request is if the field you want to update does not exist, it will create one for you.

like image 140
Ahmadreza Avatar answered Mar 10 '23 12:03

Ahmadreza