issues while posting data from curl to Firestore throw curl

Viewed 709

I am very beginner to Google Firestore (also to Firebase). I am doing a simple POC and I am suffering a bit to interact with FireStore. Well, reading the data from Angular worked as a charm so easy. Nevertheless, simple actions like add a new data from curl or deleting the collection from FireBase Cli has taken me the whole day without progress . Just to stress how dummy I am in FireStore, I have used ElasticSearch local installed so I am trying similar behaviours/tools/functionalities like posting/creating fields from curl/postman (it is not part of this question compare one another, just exposing my limitations) but I just can't move forward.

This is how I connected successfully from Angular:

  firebaseConfig : {
    apiKey: "*** removed ***",
    authDomain: "transfer-status-realtime.firebaseapp.com",
    databaseURL: "https://transfer-status-realtime.firebaseio.com",
    projectId: "transfer-status-realtime",
    storageBucket: "transfer-status-realtime.appspot.com",
    messagingSenderId: "143612370857",
    appId: "1:143612370857:web:a9d0b21e3a58520ed02eb8",
    measurementId: "G-WT5C346D7T"
  }

I have based mainly my tentatives in Making Rest Calls and other stack overflow question

My last and recent tried:

c:\temp>curl -X POST -H "Content-Type: application/json" -d'{"fields": {"status": "success"}}' "https://firestore.googleapis.com/v1beta1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=***removed***"
curl: (3) [globbing] unmatched brace in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 8
{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Closing quote expected in string.\n\n^",
    "status": "INVALID_ARGUMENT"
  }
}

So my main issue: how add data to FireStore from curl?

In case it matter, here is my database console:

FireStore Database Console

Other tentatives probably in wrong direction I have tried are:

  1. According to https://firebase.google.com/docs/database/rest/save-data, I can save fields by using PUT so I tried write a curl command and I got stuck because I don't know what would be the "fireblog" in my case?

    curl -X PUT -d '{
      "transfers": {
        "id": "idfromPut",
        "status": 1
      }
    }' 'https://transfer-status-realtime.firebaseio.com/rest/saving-data/fireblog/users.json'
    
  2. I also tried but what would be message_list in my case? curl -X POST -d '{"status" : 2}' 'https://transfer-status-realtime.firebaseio.com/message_list.json'

*** edited

c:\temp>curl -X POST -H "Content-Type: application/json" -d "{\"fields\": {\"status\": \"success\"}}" "https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=*** removed ***"
{
  "error": {
    "code": 400,
    "message": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\"",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "document.fields[0].value",
            "description": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\""
          }
        ]
      }
    ]
  }
}

In case it is relevant, same issue using external json file

C:\WSs\reactive-stack\reactive-front\src>curl -X POST -H "Content-Type: application/json" -d @load-data-tofirestore.json "https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers?&key=***removed"
{
  "error": {
    "code": 400,
    "message": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\"",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "document.fields[0].value",
            "description": "Invalid value at 'document.fields[0].value' (type.googleapis.com/google.firestore.v1.Value), \"success\""
          }
        ]
      }
    ]
  }
}

And here is the json file

{"fields": {"status": "success"}}

* SOLUTION *

from cUrl

C:\WSs\reactive-stack\reactive-front\src>curl -H "Content-Type: application/json" -X POST https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers -d "{\"fields\": { \"status\": { \"stringValue\": \"outro status\" } }}"
{
  "name": "projects/transfer-status-realtime/databases/(default)/documents/transfers/ddwHxjy7eQV3640pW0Xx",
  "fields": {
    "status": {
      "stringValue": "outro status"
    }
  },
  "createTime": "2020-04-06T16:21:16.453553Z",
  "updateTime": "2020-04-06T16:21:16.453553Z"
}

from PowerShell

PS C:\Users> $body = @"
>> {
>>   "fields": {
>>     "status": {
>>       "stringValue": "fracasso"
>>     }
>>   }
>> }
>> "@
>>
PS C:\Users\Cast> $params = @{
>>     Uri         = 'https://firestore.googleapis.com/v1/projects/transfer-status-realtime/databases/(default)/documents/transfers'
>>     Method      = 'POST'
>>     Body        = $body
>>     ContentType = 'application/json'
>> }
>>
PS C:\Users\Cast> Invoke-RestMethod @params

name                                                                                           fields     createTime
----                                                                                           ------     ----------
projects/transfer-status-realtime/databases/(default)/documents/transfers/z4TB6gzcgXMRUGR8ecek @{status=} 2020-04-06...


PS C:\Users>
1 Answers

I believe the root cause of your issue is due to cURL on a windows system. As seen in this other question, there are limitations on how the command line arguments are parsed in a windows shell.

Instead of using this:

-d'{"fields": {"status": "success"}}'

You should state your payload as:

-d "{\"fields\": {\"status\": \"success\"}}"

Another solution could be writing the JSON in a file and using its path in the cURL query. I believe you could keep the right JSON format this way, but you would be forced to use a file, which is far from optimal if you don't intend to send the same payload multiple times:

-d @file_with_data.json
Related