Miguel Lattuada
Miguel Lattuada

Reputation: 5417

Saving data to Firebase with REST

I'm using Firebase to store application tokens with names, using the REST api.

This is the data structure I'm using:

Firebase data structure

This is the data structure after I add a new application. Firebase data structure after

This is the command I use.

curl -X POST -d "{\"3ba7792jae16328\":{\"name\":\"Test 2\"}}" \ https://xxx.firebaseio.com/apps.json

I don't want the unique ID that the POST request adds to my data structure. I already tried with a PUT request but it override all the data inside the "apps" node. Anyways this is the PUT command I tried.

curl -X POST -d "{\"3ba7792jae16328\":{\"name\":\"Test 2\"}}" \ https://xxx.firebaseio.com/apps.json

Is there another way to add data and preserve my structure?

(Use the auto generated POST tokens as my own application tokens is not a valid answer)

Upvotes: 2

Views: 8887

Answers (2)

Abdelhadi Abdo
Abdelhadi Abdo

Reputation: 412

There are two ways:

  • if you want to add on row:

    method: PUT

    url: https://yourID.firebaseio.com/pages/your_object_name.json

    data:

    {
      "1": {
        "id": "5",
        "name": "page 4"
      }
    }
    
  • if you want to add multiple rows:

    methode: PATCH

    url: https://yourID.firebaseio.com/pages.json

    data:

    {
      "1": {
        "id": "5",
        "name": "page 4"
      },
      "2": {
        "id": "5",
        "name": "page 4"
      },
      "3": {
        "id": "5",
        "name": "page 4"
      }
    }
    

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599521

You should put your self-generated ID into the URL:

curl -X POST -d "{\":{\"name\":\"Test 2\"}" \ https://xxx.firebaseio.com/apps/3ba7792jae16328.json

Firebase always overwrites (unless you use PATCH) the entire node at the URL you indicate. So it's important that you specify the URL up to the level that you want to overwrite data.

Upvotes: 1

Related Questions