Home > Mobile >  How can I make firestore REST API PATCH request?
How can I make firestore REST API PATCH request?

Time:09-17

I set my Firestore simple database with one field, and I checked the the GET request according to docs:

/projects/YOUR_PROJECT_ID/databases/(default)/documents/cities/LA

But I am unable to understand patch request docs. In this patch request pattern: https://firestore.googleapis.com/v1/projects/{project_id}/databases/{database_id}/documents/{document_path}?updateMask.fieldPaths=KEYVALUE.VALUE

I am confuse, how shall I use this to make a patch request?. How can I obtain the document path? kindly guide. thanks

CodePudding user response:

You need to pass updated data in the format specified in the documentation:

fetch("https://firestore.googleapis.com/v1/projects/[PROJECT_ID]/databases/(default)/documents/[COLLECTION]/[DOC_NAME]?currentDocument.exists=true&updateMask.fieldPaths=name&alt=json", {
  headers: {
    authorization: "Bearer [ACCESS_TOKEN]",
    "content-type": "application/json",
  },
  body: JSON.stringify({ fields: { name : { stringValue : "Username"}}}),
  method: "PATCH"
});

Do note the complete request URL. It contain a query parameter updateMask.fieldPaths=name. If that's missing the the complete document will be replaced by new data. The fields property is a map and you must specify the field type as shown above. You can find more about Value types here

  • Related