3

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>
Jim C
  • 3,957
  • 25
  • 85
  • 162
  • 1
    with the solution in mind, have you tried Cygwin? the Cygwin terminal follows the linux/bash escape rules :) (it's basically a port of GNU bash on Windows) – hanshenrik Apr 09 '20 at 11:05
  • Honestly, I didn't know much about Cygwin but I will give a try soon. For a very dummy Cygwin, what is the difference between Cigwin terminal and PowerShell? They are intended for same purpose, right? Run linux commands on Windows terminal, right? – Jim C Apr 09 '20 at 14:05
  • 1
    Nope, PowerShell is about running... C-Sharp? -like Windows-commands on Windows. PowerShell has nothing to do with Linux. but Cygwin is about running Linux commands on Windows. btw while you have Cygwin around, keep the installer also. the Cygwin installer is basically your package manager, when you need new linux-programs (like `rsync` or something), you need to re-run the Cygwin setup program. it wont install everything again, it will just add the extra packages requested – hanshenrik Apr 10 '20 at 08:46

1 Answers1

3

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
Ajordat
  • 1,320
  • 2
  • 7
  • 19
  • I got "... "fieldViolations": [ { "field": "document.fields[0].value", "description": "Invalid value at 'document.fields[0].value'..." as pasted above. It seems I am not properly writing the command from Google Firestore perspective since I see "invalid value". – Jim C Apr 06 '20 at 13:24
  • 1
    @JimC I'm failing to understand this last error, what was the root cause of the second issue? The URL or the JSON's content? – Ajordat Apr 07 '20 at 07:46
  • 1
    the error "..."type.googleapis.com/google.rpc.BadRequest, fieldViolations..." was definitely caused by bad command format. If you compare may last that successed with the all others failing, I did use before "... {... "stringValue"..." in all others failling – Jim C Apr 09 '20 at 14:13