Firebase REST API - POST Request - rest

I'm trying to add records to my Firebase database as follows:
So basically we have matches -> user_supplied_id -> {id,location}
This is achievable using the following code and the Swift API:
let matches = Database.database().reference().child("users").child(UserDefaults.standard.object(forKey: "uid") as! String).child("matches").child((all_listings?[index].listingId)!)
let newBookData = [
"id": all_listings?[index].listingId,
"location" : all_listings?[index].location
] as [String : Any]
matches.setValue(newBookData)
I am now trying to replicate this behaviour using the Firebase REST API. I'm basically sending a POST request to the address:
https://PROJECTID.firebaseio.com/.../matches/-LOpJmU9yj4hAocHjnrB.json
with the following data:
"{\"id\":\"-LOpJmU9yj4hAocHjnrB\",\"location\":\"Edinburgh\"}"
However, this results in the following outcome instead:
As you can see, it creates an additional ID and level of nesting before adding the elements to the database. How can I fix this?

Don't use POST. According to the documentation:
To accomplish the equivalent of the JavaScript push() method (see Lists of Data), you can issue a POST request.
You don't want a push here. A push operation creates a new random push ID and makes that the key of the data you provided.
If already you know the location you want to set (it looks like you already have a known push id), just use a PUT to set the data at that location.

Related

How to use update mask to update a 'arrayValue' at an index?

I need to update a particular field inside a document in Firebase. My application is WatchOS 6 application and hence I am sort of forced to use the REST API and uploadMask.
I have the following URL that points to the document that needs to be updated along with the field mask name that I need to update:
https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/users/{documentID}/goals/{documentID}/tasks/{documentID}?updateMask.fieldPaths=is_complete
Now, my request body:
{
"fields": {
"instructions&steps": {
"arrayValue": {
"values": [
{
"mapValue": {
"fields": {
"is_complete": true
}
}
}
]
}
}
}
}
I'll explain how I came up with the request body above:
The document has multiple values, one of them is fields. Inside fields, again I have multiple values. The one I want to update is 'instructions&steps' which is an array of JSON objects.
I need to be able to update any index of that array and the field 'is_completed' on that particular index for my application.
I am not sure how to do that.
I tried figuring it out on the Google API Explorer with the above mentioned body and URL, I get a 400 error with message: document.fields[0].value.array_value.values[0].map_value.fields[0].value
Why does it say fields[0] ? That is not an array.
Ideally, it should be something like: document.fields.instructions&steps.array_value.values[0].map_value.fields.is_completed
And if I want to update for 2nd object, I simply need:
document.fields.instructions&steps.array_value.values[1].map_value.fields.is_completed
Any help is appreciated. Thanks.
EDIT 1:
Okay, so I was able to get the request working by changing "is_complete": true to "is_complete" : {booleanValue: true}. I now get a 200 status and the entire document in response, but the value of field is_complete is still false and isn't updated! Additionally, how do I specify the index of the JSON at which I want to update is_completed field ?
After doing some additional digging, apparently Firebase doesn't allow you to update individual values in arrayValues yet. Is this true ?
If so, is there some way to work around that since my platform(Apple Watch/ watchOS 6) doesn't have Firebase SDK yet and I have to use REST API because of that.

Obtain specific data from Google Firestore using Rest API calls (HTTP-GET)

Problem
I want to retrieve specific data from Google Firestore.
It's only possible to get all of the 'Fields' data. But no specific data within fields
Example of the GET-Request:
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields
As you can see, It is possible to obtain all the items in the object 'Fields'. But it is not possible to get any further into detail to obtain more specific data (test, message, nodeID, ...)
Tryouts
I have already tried:
fields=fields/test
fields=fields.test
fields=fields(test)
fields=fields/test/integerValue
...
Expected Results
I want to obtain specific data like the String / integer value of my objects in 'Fields'.
This example should return the integerValue with 30
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields/test
This example should return 30
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields/test/integerValue
Solution
While browsing the web, I came across Google Api Explorer:
https://developers.google.com/apis-explorer/#search/firestore/firestore/v1beta1/
When trying out some possibilities, I came across this:
https://firestore.googleapis.com/v1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?mask.fieldPaths=nodeID&fields=fields&key={YOUR_API_KEY}
This gives me the right information
but I still need a more detailed form of this answer like only the 'EduBox-1234567'
The way to retrieve a specific field is to use mask.fieldPaths. For example the following GET method:
https://firestore.googleapis.com/v1beta1/projects/edubox-49528/databases/(default)/documents/nodes/EduBox-1234567?key=[My_API_KEY]&fields=fields&mask.fieldPaths=nodeID
is going do return:
{
"fields": {
"nodeID": {
"stringValue": "EduBox-1234567"
}
}
Documentation references here and here.

How to fix 400 Bad Request with empty filter query in RestHeart?

I am using Restheart and MongoDB. And I am calling one service(API) for the response data.
Working API
http://127.0.0.1:8080/test/donor?filter="{'name':'john'}"
When I calling above API then it is working, But When I putted API filter is empty, Like filter={} then it is not working.
Not Working API
http://127.0.0.1:8080/test/donor?filter="{}"
When I calling API with empty filter="{}", Then its giving me 400 Bad Request
Actually I wan to achieve one API Call for two purpose.
One for with filter condition.
Second one without filter condition.
I want to call Like below.
var qryFilter = {};
var qryFilterParam;
qryFilter["color.code"] = dateInParameter.code;
qryFilterParam = '&filter=' + JSON.stringify(qryFilter)
http://127.0.0.1:8080/test/donor?qryFilterParam
Then some time qryFilterParam have value and some time don't have. When Its have values like filter="{'name':'john'}" then it is working but when it's don't have key value like filter="{}" then it is not working. I am searching solution on website http://restheart.org/curies/1.0/filter.html but I am not able find to solution.
An empty filter is not allowed by restheart.
You need an if statement in your code to make a request with filter qparam and a request without.

Ordering Server adding sshkeys

I want to order server with sshkeys using the api, but when I use the sshkey property in the structure it returns the result without the keys, I know my code is working fine becausw I orderwd before. I would like to check if my ids are correct, is there any form to check them by using my label names???
this is the structure for ssh keys:
"sshKeys": [
{
"sshKeyIds": [94206]
}
]
You can call http://sldn.softlayer.com/reference/services/SoftLayer_Account/getSshKeys method to get the IDs of your sshs keys and you can use object filters to get the ssks by label this is am example using Rest:
GET https://<USERNAME>:<APIKEY>#api.softlayer.com/rest/v3/SoftLayer_Account/getSshKeys?objectFilter={"sshKeys":{"label":{"operation":"tonny"}}}
here more information about object filters http://sldn.softlayer.com/article/object-filters

Graph API - get JSON data without "paging" key

I'm getting facebook data using graph api, adding fields in string and get JSON result.
Example:
https://graph.facebook.com/me?fields=music
But JSON returned contains a "paging" key and I do not I want this key.
{ "music":{
"data":[
{
"name":"",
"category":"",
"id":"",
"created_time":""
},
{
"name":"",
"category":"",
"id":"",
"created_time":""
}
],
"paging":{
"next":"https://graph.facebook.com/me?fields=music&method=GET&metadata=true&format=json&callback=___GraphExplorerAsyncCallback___&access_token=...&limit=5000&offset=5000&__after_id=..."
}}}
EDITED:
I'm using Java API (restfb.com) to get JSON.
The command in java is:
FacebookClient client = new DefaultFacebookClient("ACCESS_TOKEN_HERE");
JsonObject rMusic = client.fetchObject("ID_HERE", JsonObject.class, Parameter.with("fields", "id,name,religion,birthday,music"));
How do I avoid it or remove it?
When you have your Javascript object built from the JSON, just pay attention to the array of data: result.music.data
And forget about the paging property: result.music.paging
Remember, there's no law in coding that you have to look at every property in your scripts.
Based upon the edit to the question above, here's a new answer.
The Rest API is deprecated. You should upgrade your app to use the Graph API as this is the one being supported.
Also, if you see a property you don't like, you don't have to access it. Remember, there's no law in coding that you have to look at every property in your scripts.